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
Hunting Vulnerabilities in Symfony with LLMs
vinceamstoutz
0
560
Creating Composable Callables in Contemporary C++
rollbear
0
170
Make SRE Operations Easier with Azure SRE Agent
kkamegawa
0
7.9k
Language Server 使ってる? 〜VSCode と Zed の場合〜 / Are you using a Language Server? ~For VS Code and Zed~
handlename
0
800
エンジニア向け会社紹介/Findy Company Profile
findyinc
6
350k
OSもどきOS
arkw
0
590
気づいたらRubyで100作品 ー クリエイティブコーディングが生活の一部になるまで / 100 Ruby Sketches Later: How Creative Coding Became Part of My Life
chobishiba
3
610
不変条件と整合性境界—ビジネスが決める設計判断と実現パターン / Invariants and Consistency Boundaries
nrslib
14
5.8k
Developing with AI Agents — Codex, Claude Code & Cowork Practical Guide
x5gtrn
PRO
0
1.3k
Contextとはなにか
chiroruxx
1
370
New "Type" system on PicoRuby
pocke
1
1k
エンジニアと一緒にテストコードの設計と実装を改善した話
mototakatsu
0
220
Featured
See All Featured
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
170
XXLCSS - How to scale CSS and keep your sanity
sugarenia
250
1.3M
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
170
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.8k
Visualization
eitanlees
152
17k
Producing Creativity
orderedlist
PRO
348
40k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
330
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
200
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
200
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Ethics towards AI in product and experience design
skipperchong
2
310
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
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