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

Objective-C & Swift: Interoperability Tips and ...

Objective-C & Swift: Interoperability Tips and Tricks

Talk given at Wizeline Academy in Mexico City.

Oscar Swanros

May 19, 2018
Tweet

More Decks by Oscar Swanros

Other Decks in Technology

Transcript

  1. • You already know Objective-C • ABI stability • Flexibility

    • Less work when trying to interact with low- level APIs.
  2. • Legacy project trying to incorporate new features? • New

    project using legacy dependencies? • Someone on your team doesn't know one of the two?
  3. @objc • Expose Swift API to Objective-C • Make concrete

    Swift interfaces for Objective-C • Access the Objective-C runtime from Swift
  4. class WZManager { init(identifier: WZIdentifier) { // ... } func

    register(observer: WZObservable) { // ... } } Exposing API
  5. @objc class WZManager { @objc init(identifier: WZIdentifier) { // ...

    } @objc func register(observer: WZObservable) { // ... } } Exposing API All types involved need to be representable in Objective-C
  6. Access the Objective-C Runtime private extension Selector { static let

    didTapLoginButton = #selector(VC.didTapLoginButton) } class VC: UIViewController { @objc func didTapLoginButton() { } override func viewDidLoad() { super.viewDidLoad() button.addTarget(self, action: .didTapLoginButton, action: .to } }
  7. // Base definition typedef NSString *WZShirtPattern NS_TYPED_EXTENSIBLE_ENUM; // Specializations extern

    WZShirtPattern const WZShirtPatternFlowers; extern WZShirtPattern const WZShirtPatternPlaid; extern WZShirtPattern const WZShirtPatternSquares; extern WZShirtPattern const WZShirtPatternDots; extern WZShirtPattern const WZShirtPatternLines; struct ShirtPattern { typealias RawValue = String init(_ rawValue: RawValue) init(rawValue: rawValue) var rawValue: RawValue { get } static var flowers: ShirtPattern { get } static var plaid: ShirtPattern { get } static var squares: ShirtPattern { get } static var dots: ShirtPattern { get } static var lines: ShirtPattern { get } }
  8. // Base definition typedef NSString *WZShirtPattern NS_TYPED_EXTENSIBLE_ENUM; // Specializations extern

    WZShirtPattern const WZShirtPatternFlowers; extern WZShirtPattern const WZShirtPatternPlaid; extern WZShirtPattern const WZShirtPatternSquares; extern WZShirtPattern const WZShirtPatternDots; extern WZShirtPattern const WZShirtPatternLines; struct ShirtPattern { typealias RawValue = String init(_ rawValue: RawValue) init(rawValue: rawValue) var rawValue: RawValue { get } static var flowers: ShirtPattern { get } static var plaid: ShirtPattern { get } static var squares: ShirtPattern { get } static var dots: ShirtPattern { get } static var lines: ShirtPattern { get } }
  9. struct ShirtPattern { typealias RawValue = String init(_ rawValue: RawValue)

    init(rawValue: rawValue) var rawValue: RawValue { get } static var flowers: ShirtPattern { get } static var plaid: ShirtPattern { get } static var squares: ShirtPattern { get } static var dots: ShirtPattern { get } static var lines: ShirtPattern { get } } extension ShirtPattern { static var bubbles: ShirtPattern { // generate custom pattern } }
  10. If you need to mix the two… • Think about

    your use case • Use the tools a your disposal, see how open source projects manage this • Think seriously about starting with Objective-C, then adding Swift • Consider that Objective-C is more compatible with Swift than Swift is with Objective-C. • Organize your project neatly!