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

Swift, meet Objective-C

Swift, meet Objective-C

Danielle Tomlinson

March 22, 2015
Tweet

More Decks by Danielle Tomlinson

Other Decks in Programming

Transcript

  1. !

  2. Starting out Bridging Header // // Use this file to

    import your target's public headers that you would like to expose to Swift. // #import "Person.h" #import "PersonController.h" #import "CoreDataController.h"
  3. The bridging header // ReactiveCocoa-Bridging-Header.h // // Use this file

    to import your target's public headers that you would like to expose to Swift. // #import "RACCommand.h" #import "RACDisposable.h" #import "RACEvent.h" #import "RACScheduler.h" #import "RACTargetQueueScheduler.h" #import "RACSignal.h" #import "RACSignal+Operations.h" #import "RACStream.h" #import "RACSubscriber.h"
  4. Method Names // Objective-C - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions; //

    Swift func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
  5. Nullability Before // Objective-C @interface XYZList : NSObject <NSCoding, NSCopying>

    - (XYZListItem *)itemWithName:(NSString *)name; @property (copy, readonly) NSArray *allItems; @end // Swift class XYZList : NSObject, NSCoding, NSCopying { func itemWithName(name: String!) -> XYZListItem! @NSCopying var allItems: [AnyObject]! { get } }
  6. Nullability After // Objective-C @interface XYZList : NSObject <NSCoding, NSCopying>

    - (XYZListItem * __nullable)itemWithName:(NSString * __nonnull)name; @property (copy, readonly) NSArray * __nonnull allItems; @end // Swift class XYZList : NSObject, NSCoding, NSCopying { func itemWithName(name: String) -> AAPLListItem? @NSCopying var allItems: [AnyObject] { get } }
  7. Classes // Swift @objc class MySwiftNSObject { var array: [String]

    init(array: [String]) { self.array = array; } } // Objective-C @interface MySwiftNSObject @property (nonatomic, copy) NSArray * __nonnull array; - (instancetype)initWithArray:(NSArray * __nonnull)array OBJC_DESIGNATED_INITIALIZER; @end
  8. Methods func genericFunction<T>(param: T) // Not bridged func defaultParameterFunction(param: String

    = "Hi") // Not bridged func function(param: String) - (void)function:(NSString * __nonnull)param;
  9. Properties // Swift let forname: String var surname: String //

    Objective-C @property (nonatomic, readonly, copy) NSString * __nonnull forname; @property (nonatomic, copy) NSString * __nonnull surname;
  10. Properties KVO (Key Value Observing) dynamic var name: String object.addObserver(self,

    forKeyPath: "name", options: .New, context: &myContext)
  11. Enums @objc enum MyEnum : Int { case SomeValue =

    0 case AnotherValue = 1 } typedef NS_ENUM(NSInteger, MyEnum) { MyEnumSomeValue = 0, MyEnumAnotherValue = 1 };
  12. Bridging limitations !!! No: - Structs - Functions - Operators

    - Generics - Subclassing Swift classes in objective-c
  13. Further Resources Apple • WWDC Session 406 (Integrating Swift with

    Objective-C) • WWDC Session 407 (Swift Interoperability in Depth) Blog Posts • https://mikeash.com/pyblog/friday-qa-2014-08-15-swift- name-mangling.html