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

Wrapping Objective-C Frameworks in Swift

JP Simard
February 07, 2015

Wrapping Objective-C Frameworks in Swift

Wherein we discuss the trials and tribulations endured while wrapping Realm's Objective-C interface in a Swift framework. You'll learn how to craft the perfect Swift interface without duplicating core functionality, as well as tricks to work around tooling limitations. Why let Objective-C die in sadness when we can just hide it in a shiny new Swift suit?

JP Simard

February 07, 2015
Tweet

More Decks by JP Simard

Other Decks in Programming

Transcript

  1. Why? API Improvements // Go from this: func objcFunc(a: AnyObject!,

    b: NSArray!) -> NSString! // to this: func swiftFunc(a: ClassA?, b: [Int]) -> String? // Or using autoclosures, generics, // default parameter values, making types // and optionality explicit, etc. 3
  2. Why? To Expose Unmappable Interfaces // Objective-C variable arguments aren't

    exposed to Swift + (RLMResults *)objectsWhere:(NSString *)format, ...; // neither are #defines #define NSStringFromBool(b) (b ? @"YES" : @"NO") // or certain declarations NSVariableStatusItemLength 4
  3. Why Not Rewrite in Swift? 1. Lazy 2. No significant

    gains from the language 3. Existing codebase complex and/or mature 4. Yup, still lazy 5
  4. Porting an API RLMObject // ObjC Realm.Object // Swift RLMObject.allObjects()

    // ObjC objects<T: RLMObject>(type: T.Type) // Swift 7
  5. Exposing Just the Right Interfaces Making sure Objective-C is still

    import-able 1. Module Map needs to be preserved 2. Must be a dynamic framework 10
  6. Exposing Just the Right Interfaces Exposing private interfaces just for

    Swift binding Unfortunately there is no private bridging headers for framework targets 13
  7. Exposing Just the Right Interfaces Exposing private interfaces just for

    Swift binding framework module Realm { umbrella header "Realm.h" export * module * { export * } explicit module Private { header "RLMRealm_Private.h" } } 14
  8. Exposing Just the Right Interfaces Don't subclass import Realm class

    Realm: RLMRealm { // What's wrong with this? } 15
  9. Exposing Just the Right Interfaces Don't subclass. Wrap! import Realm

    class Realm { private var rlmRealm: RLMRealm private init(rlmRealm: RLMRealm) { self.rlmRealm = rlmRealm } } 16
  10. Documentation 1. Compare Objective-C & Swift interfaces side by side

    2. Copy docs from Objective-C 3. Convert doxygen format to ReST 18