Swift, meet Objective-C

Swift, meet Objective-C

8e7d17f63cb19895a31b4ba6a976e781?s=128

Danielle Tomlinson

March 22, 2015
Tweet

Transcript

  1. ! Swift, meet Objective-C ! Twitter: @DanToml Email: Dan@Tomlinson.io

  2. !

  3. In the sharp, square bracketed embrace of Objective-C — @AndrewJClark

  4. All objective-c written now is technical debt

  5. ...but so is all of your Swift.

  6. You can use them together

  7. A whole bunch of new developers are going to be

    Swift only.
  8. Starting out

  9. 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"
  10. Objective-C -> Swift

  11. 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"
  12. Method Names // Objective-C - (instancetype)initWithArray:(NSArray *)array title:(NSString *)title; //

    Swift init!(array: [AnyObject]!, title: String!)
  13. Method Names // Objective-C - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions; //

    Swift func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
  14. Nullability • __nullable • __nonnull • __null_resettable // Properties only.

    • null_unspecified // Default.
  15. 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 } }
  16. 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 } }
  17. Swift -> Objective-C

  18. What can be bridged? • Classes

  19. 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
  20. What can be bridged? • Classes • Protocols

  21. Protocols @objc protocol Edible { func eat() } @protocol Edible

    { - (void)eat; }
  22. What can be bridged? • Classes • Protocols • Methods

  23. Methods func genericFunction<T>(param: T) // Not bridged func defaultParameterFunction(param: String

    = "Hi") // Not bridged func function(param: String) - (void)function:(NSString * __nonnull)param;
  24. What can be bridged? • Classes • Protocols • Methods

    • Properties
  25. Properties // Swift let forname: String var surname: String //

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

    forKeyPath: "name", options: .New, context: &myContext)
  27. What can be bridged? • Classes • Protocols • Methods

    • Properties • Enums
  28. Enums @objc enum MyEnum : Int { case SomeValue =

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

    - Generics - Subclassing Swift classes in objective-c
  30. Roundup

  31. 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
  32. Questions? Twitter: @DanToml Email: Dan@Tomlinson.io