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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
1B+ /day規模のログを管理する技術
broadleaf
0
110
エンジニア向け会社紹介/Findy Company Profile
findyinc
6
350k
そのテスト、説明できますか?~LWテスト戦略FW~のご紹介
nakahara
0
160
「AIで開発し、AIを届ける」をEvalでつなぐ 〜AIネイティブに始めるプロダクト開発の実践〜 / Connecting "Develop with AI, deliver AI" with Eval
rkaga
4
5.4k
LaravelLive Japan の裏方のすべて — 第188回 PHP勉強会@東京 (2026-06-24)
suguruooki
2
120
ローカルLLMを使ってB2Bサービスを作っていての学び
yaotti
0
210
並列実装の現場、2ヶ月間実務でAIを使い倒したAIもPCも私も限界が近い
ming_ayami
0
130
スマートグラスで並列バイブコーディング
hyshu
0
260
OSもどきOS
arkw
0
590
TSKaigi Night Talks 2026_TypeScriptでサプライチェーンの整合性を型に閉じ込める
geekplus_tech
0
410
はてなアカウント基盤 State of the Union
cockscomb
0
680
不変条件と整合性境界—ビジネスが決める設計判断と実現パターン / Invariants and Consistency Boundaries
nrslib
14
5.8k
Featured
See All Featured
So, you think you're a good person
axbom
PRO
2
2.1k
Code Reviewing Like a Champion
maltzj
528
40k
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
470
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
390
Marketing to machines
jonoalderson
1
5.5k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.5k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.2k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
2.9k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
2.9k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
11k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
210
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
1
3.6k
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