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
Creating Composable Callables in Contemporary C++
rollbear
0
170
TypeScript+Orvalで実現する型安全かつ堅牢でスケーラブルなマルチチャネル通知基盤 / TSKaigi Night talks ~after conference~
d0riven
0
360
Mujeres en SEO Summit 2026 - Greatest Disaster Hits en Web Performance
guaca
0
200
過去最大のMCPアップデート! 2026-07-28 RC版の謎に迫る
licux
6
390
Observability in Practice:Grafana 與 Edge Device SRE 的那些事
blueswen
0
170
PHPで使える日時の表現と、その知り方 #frontend_phpcon_do
o0h
PRO
0
260
AIだと陥りがちなJakarta EE最新技術への移行時の落とし穴と解決策
tnagao7
0
120
IBM Bobを活用したレガシーアプリの最新化
oniak3ibm
PRO
1
210
Developing with AI Agents — Codex, Claude Code & Cowork Practical Guide
x5gtrn
PRO
0
1.3k
さぁV100、メモリをお食べ・・・
nilpe
0
150
はてなアカウント基盤 State of the Union
cockscomb
0
690
Oxlintのカスタムルールの現況
syumai
6
1.2k
Featured
See All Featured
AI: The stuff that nobody shows you
jnunemaker
PRO
8
730
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.1k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
ラッコキーワード サービス紹介資料
rakko
1
3.7M
Joys of Absence: A Defence of Solitary Play
codingconduct
1
400
Measuring & Analyzing Core Web Vitals
bluesmoon
9
870
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
400
Building a Scalable Design System with Sketch
lauravandoore
463
34k
Utilizing Notion as your number one productivity tool
mfonobong
4
330
We Have a Design System, Now What?
morganepeng
55
8.2k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
420
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