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.3k
Playing Sheet presentation style on iOS 13
scenee
0
690
Fluid Interfacesの実践
scenee
4
1.7k
Introducing Relax
scenee
0
620
Potatotips#7: Box C String Tip
scenee
0
2.8k
Other Decks in Programming
See All in Programming
AI 輔助遺留系統現代化的經驗分享
jame2408
1
990
AI時代のUIはどこへ行く?その2!
yusukebe
22
7.5k
不変条件と整合性境界—ビジネスが決める設計判断と実現パターン / Invariants and Consistency Boundaries
nrslib
14
5.8k
Agentic UI
manfredsteyer
PRO
0
200
鹿野さんに聞く!『TypeScriptコードレシピ集』で磨く実践力
tonkotsuboy_com
2
750
Oxcを導入して開発体験が向上した話
yug1224
4
340
JavaDoc 再入門
nagise
1
420
技術記事、 専門家としてのプログラマ、 言語化
mizchi
13
6.5k
コンテキストの使い捨てをやめる — ビジネスルール駆動開発と miko —
ioki
0
230
Vue × Nuxt × Oxc どこまで使える?実運用の現在地
andpad
0
300
LaravelLive Japan の裏方のすべて — 第188回 PHP勉強会@東京 (2026-06-24)
suguruooki
2
120
決定論的オーケストレーションの設計と実装 / Design and Implementation of Deterministic Orchestration
nrslib
4
1.5k
Featured
See All Featured
The World Runs on Bad Software
bkeepers
PRO
72
12k
Six Lessons from altMBA
skipperchong
29
4.3k
Writing Fast Ruby
sferik
630
63k
Statistics for Hackers
jakevdp
799
230k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
170
A designer walks into a library…
pauljervisheath
211
24k
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
200
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
490
[SF Ruby Conf 2025] Rails X
palkan
2
1.1k
The untapped power of vector embeddings
frankvandijk
2
1.8k
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2k
Tell your own story through comics
letsgokoyo
1
970
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