Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SwiftライブラリのObjC対応における落とし穴と回避策

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.

 SwiftライブラリのObjC対応における落とし穴と回避策

Avatar for Shin Yamamoto

Shin Yamamoto

November 11, 2019
Tweet

More Decks by Shin Yamamoto

Other Decks in Programming

Transcript

  1. @objc open class FloatingPanelController: UIViewController { @objc public var isInteractionEnabled:

    Bool { @objc(setInteractionEnabled:) set { } @objc(isInteractionEnabled) get { } } @objc(trackScrollView:) public func track(scrollView: UIScrollView?) { } }
  2. 1. String Enum͕ఆٛͰ͖ͳ͍ • @objc͸ɺInt EnumͰ͔͠෇༩Ͱ͖ͳ͍ • Publicͷ@objc APIʹ͓͍ͯDictionary Keyʹ

    Int Enum͸࢖͑ͳ͍ • Int -> NSInteger (not NSNumber) • Swift͚ͩͰѻ͏ͱ͖͸໰୊ͳ͍
  3. #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
  4. #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
  5. 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), ] }
  6. - (NSDictionary<FloatingPanelState, id<FloatingPanelLayoutAnchoring>> *)layoutAnchors { return @{ FloatingPanelStateFull: [[FloatingPanelLayoutAnchor alloc]

    initWithAbsoluteInset:0.0], FloatingPanelStateTip: [[FloatingPanelLayoutAnchor alloc] initWithAbsoluteInset:44.f], }; }
  7. #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ʹ͢Δ
  8. 2. SubclassԽͰ͖ͳ͍ • SwiftͰఆٛ͞ΕͨClass͸ɺObjCͰSubclassԽͰ͖ ͳ͍ • എܠͱཧ༝͸ʁ • “Cannot subclass

    a class that was declared with the ‘objc_subclassing_restricted’ attribute”
  9. 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Ͱ͸ͳ͘ͳΔ
  10. 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
  11. 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