Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
SwiftライブラリのObjC対応における落とし穴と回避策
Search
Shin Yamamoto
November 11, 2019
Programming
340
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
SwiftライブラリのObjC対応における落とし穴と回避策
Shin Yamamoto
November 11, 2019
More Decks by Shin Yamamoto
See All by Shin Yamamoto
Meet CoreDevice and devicectl
scenee
0
630
DocCのドキュメントをGithub Pagesで公開する
scenee
2
720
Testing a constraint-based layout of UIView
scenee
1
1.4k
Playing Sheet presentation style on iOS 13
scenee
0
700
Fluid Interfacesの実践
scenee
4
1.7k
Introducing Relax
scenee
0
630
Potatotips#7: Box C String Tip
scenee
0
2.8k
Other Decks in Programming
See All in Programming
『コードを書く以外の』エンジニアリング〜課金基盤移行プロジェクト推進のためのTips4選
yuriko1211
0
510
Terraform標準の組織で AWS CDKをどう使うか
mu7889yoon
0
290
Hatena Engineer Seminar #37「言語モデルの活用に関する研究」
slashnephy
0
520
광주소프트웨어마이스터고등학교 DevFest 특강 - 바이브 코딩 시대에서 주니어 개발자로 살아남는 방법
utilforever
1
140
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
270
コーディングルールの鮮度を保ちたい for SRE NEXT 2026 / keep-fresh-go-internal-conventions-sre-next-2026
handlename
0
140
Apache Hive: Toward a Cloud Native Lakehouse
okumin
0
130
壊れたパーサから始める関数型設計と構成的なパーサ #fp_matsuri
raiga0310
2
310
才能?センス?知らん、 続けたもん勝ちだ。-- 結婚・出産・癌を越えてなお、私がプロダクトを創り続ける理由
16bitidol
2
870
AI駆動開発を妨げる技術的負債の解消アプローチ / ai-refactoring-approach
minodriven
17
9.1k
はてなアカウント基盤 State of the Union
cockscomb
1
1.3k
エンジニアにデザインハーネスを 〜デザインプロセスを規定するためのハーネス〜 / Design harness from an engineer's perspective
rkaga
2
1.5k
Featured
See All Featured
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
420
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.1k
Visualization
eitanlees
152
17k
The browser strikes back
jonoalderson
0
1.4k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
123
22k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
10k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.6k
ラッコキーワード サービス紹介資料
rakko
1
3.9M
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
390
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
550
BBQ
matthewcrist
89
10k
The World Runs on Bad Software
bkeepers
PRO
72
12k
Transcript
SwiftϥΠϒϥϦͷObjCରԠ ʹ͓͚Δམͱ݀͠ͱճආࡦ 2019/11/11 potatotips #66 Shin Yamamoto @scenee
എܠ Swift 5.1: ABI/Module Stability • SwiftʹΑΔBinary Frameworkͷ։ൃɾϝϯς ϯε͕༰қʹ •
༻ϥΠϒϥϦͷObjC -> SwiftͷҠߦ
ͳͥObjCରԠ͕ඞཁͳͷ͔ʁ • SwiftϥΠϒϥϦͰɺObjCରԠ͢ΔͱҎԼͷϓϥο τϑΥʔϜͰར༻Ͱ͖ɺϢʔβʔϕʔε͕͕Δ • ReactNative • Kotlin/Native • Xamarin
• C++/Objective-CͷΈͷΞϓϦ
@objcΛ༩͢Δ͚ͩͰʁ • @objcଐੑΛPublic APIʹ༩ • @objcͰ໋໊มߋ • ObjCͷ໋໊نʹԊ͏Α͏ʹ • BooleanͳͲgetter/setter͝ͱʹ
@objc open class FloatingPanelController: UIViewController { @objc public var isInteractionEnabled:
Bool { @objc(setInteractionEnabled:) set { } @objc(isInteractionEnabled) get { } } @objc(trackScrollView:) public func track(scrollView: UIScrollView?) { } }
ઃܭ࣍ୈͰ @objcଐੑͷΈͰෆे
3ͭͷམͱ݀͠ 1. String Enum͕ఆٛͰ͖ͳ͍ 2. SubclassԽ͕Ͱ͖ͳ͍ 3. Protocol Extensionͷҙਤ͠ͳ͍ಈ࡞(᠘)
1. String Enum͕ఆٛͰ͖ͳ͍ • @objcɺInt EnumͰ͔͠༩Ͱ͖ͳ͍ • Publicͷ@objc APIʹ͓͍ͯDictionary Keyʹ
Int Enum͑ͳ͍ • Int -> NSInteger (not NSNumber) • Swift͚ͩͰѻ͏ͱ͖ͳ͍
ղܾࡦ ObjCͰString EnumΛఆٛ 1. ObjCͰString EnumΛఆٛ 2. Umbrella headerͰimport 3.
SwiftͰར༻ɾ֦ு
https://developer.apple.com/videos/play/wwdc2018/408/
Umbrella header
#import <Foundation/Foundation.h> NS_ASSUME_NONNULL_BEGIN typedef NSString * FloatingPanelState NS_TYPED_EXTENSIBLE_ENUM NS_SWIFT_NAME(FloatingPanelState); FOUNDATION_EXPORT
FloatingPanelState FloatingPanelStateFull; FOUNDATION_EXPORT FloatingPanelState FloatingPanelStateHalf; FOUNDATION_EXPORT FloatingPanelState FloatingPanelStateTip; FOUNDATION_EXPORT FloatingPanelState FloatingPanelStateHidden; NS_ASSUME_NONNULL_END #import "FloatingPanelState.h" FloatingPanelState FloatingPanelStateFull = @"Full"; FloatingPanelState FloatingPanelStateHalf = @"Half"; FloatingPanelState FloatingPanelStateTip = @"Tip"; FloatingPanelState FloatingPanelStateHidden = @"Hidden"; FloatingPanelState.m FloatingPanelState.h
#ifndef FloatingPanel_h #define FloatingPanel_h #import <UIKit/UIKit.h> #import <FloatingPanel/FloatingPanelState.h> FOUNDATION_EXPORT double
FloatingPanelVersionNumber; FOUNDATION_EXPORT const unsigned char FloatingPanelVersionString[]; #endif /* FloatingPanel_h */ FloatingPanel.h
extension FloatingPanelState: CaseIterable { public static var allCases: [FloatingPanelState] {
return [.full, .half, .tip, .hidden] } } FloatingPanel.swift
open var layoutAnchors: [FloatingPanelState: FloatingPanelLayoutAnchoring] { return [ .full: FloatingPanelLayoutAnchor(absoluteInset:
18.0, edge: .top), .half: FloatingPanelLayoutAnchor(fractionalInset: 0.5, edge: .bottom), .tip: FloatingPanelLayoutAnchor(absoluteInset: 69.0, edge: .bottom), ] }
- (NSDictionary<FloatingPanelState, id<FloatingPanelLayoutAnchoring>> *)layoutAnchors { return @{ FloatingPanelStateFull: [[FloatingPanelLayoutAnchor alloc]
initWithAbsoluteInset:0.0], FloatingPanelStateTip: [[FloatingPanelLayoutAnchor alloc] initWithAbsoluteInset:44.f], }; }
#import <Foundation/Foundation.h> NS_ASSUME_NONNULL_BEGIN typedef NSString * FloatingPanelState NS_TYPED_EXTENSIBLE_ENUM NS_SWIFT_NAME(FloatingPanelState); FOUNDATION_EXPORT
FloatingPanelState FloatingPanelStateFull; FOUNDATION_EXPORT FloatingPanelState FloatingPanelStateHalf; FOUNDATION_EXPORT FloatingPanelState FloatingPanelStateTip; FOUNDATION_EXPORT FloatingPanelState FloatingPanelStateHidden; NS_ASSUME_NONNULL_END #import "FloatingPanelState.h" FloatingPanelState FloatingPanelStateFull = @"Full"; FloatingPanelState FloatingPanelStateHalf = @"Half"; FloatingPanelState FloatingPanelStateTip = @"Tip"; FloatingPanelState FloatingPanelStateHidden = @"Hidden"; FloatingPanelState.m FloatingPanelState.h ඞͣPublic headerʹ͢Δ
Umbrella headerͷԠ༻ • C/ObjCͷ࣮ΛϥΠϒϥϦʹऔΓࠐΈ͍͢ • ґଘؔҰํ: • Swift -use-> ObjC
• ͨͩ͠ɺܕ͚ͩͰ͋Ε@class/@protocol એݴͯ͠ར༻Մೳ
2. SubclassԽͰ͖ͳ͍ • SwiftͰఆٛ͞ΕͨClassɺObjCͰSubclassԽͰ͖ ͳ͍ • എܠͱཧ༝ʁ • “Cannot subclass
a class that was declared with the ‘objc_subclassing_restricted’ attribute”
ճආࡦ • @objc protocolͰΠϯλϑΣʔεΛެ։͢Δ • ઃఆΦϒδΣΫτΛఆٛ͢Δ • ObjCͰ࣮͢Δ (Umbrella headerͰެ։)
3. Protocol Extensionͷ᠘ • Protocol Extensionͷ࣮͕ObjC͔Βݟ͑ͳ͍ • @objc protocolͷoptional methodʹProtocol
ExtensionͰσϑΥ ϧτ࣮༩ • Warningൃੜ: non-‘@objc’ method does not satisfy optional requirement of ‘@objc’ protocol • optional methodͷ࣮͕ඞཁʹͳΔ -> optionalͰͳ͘ͳΔ
Protocol Extensionͷ᠘ • SwiftͰར༻͢Δͱ͖classܧঝͰҎԼͷ͕ൃੜ͢Δ • SR-103 Protocol Extension: function’s implementation
cannot be overridden by a subclass - Swift — https:// bugs.swift.org/browse/SR-103 • SwiftͷϓϩτίϧΤΫεςϯγϣϯͷ᠘ - Qiita — https://qiita.com/omochimetaru/items/ 17cdbb5a77972c82a781
ճආࡦ Protocol ExtensionΛΘͳ͍ • Public ProtocolͷσϑΥϧτ࣮༻్ͰΘͳ͍ • ϢʔβʔαΠτʹӨڹΛ༩͑ͳ͍ϩδοΫͷ࣮ ͳ͍
·ͱΊ • ObjCରԠͰϥΠϒϥϦͷϢʔεέʔε͕͕Δ (ಛʹReactNative, Kotlin/NativeͳͲ) • ObjCରԠ@objc༩͚ͩͰෆे • ͍͔ͭ͘ͷ੍APIઃܭʹΑͬͯରԠՄೳ
References • Binary Frameworks in Swift - WWDC 2019 -
https:// developer.apple.com/videos/play/wwdc2019/416/ • Migrating Your Objective-C Code to Swift - https:// developer.apple.com/documentation/swift/migrating_your_objective- c_code_to_swift • Swift and Objective-C Interoperability - WWDC 2015 - https:// developer.apple.com/videos/play/wwdc2015/401/ • https://github.com/apple/swift-evolution/blob/master/proposals/0160- objc-inference.md
Thank you