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. ! Swift, meet Objective-C !
    Twitter: @DanToml
    Email: [email protected]

    View Slide

  2. !

    View Slide

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

    View Slide

  4. All objective-c written now is technical debt

    View Slide

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

    View Slide

  6. You can use them together

    View Slide

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

    View Slide

  8. Starting out

    View Slide

  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"

    View Slide

  10. Objective-C -> Swift

    View Slide

  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"

    View Slide

  12. Method Names
    // Objective-C
    - (instancetype)initWithArray:(NSArray *)array title:(NSString *)title;
    // Swift
    init!(array: [AnyObject]!, title: String!)

    View Slide

  13. Method Names
    // Objective-C
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
    // Swift
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool

    View Slide

  14. Nullability
    • __nullable
    • __nonnull
    • __null_resettable // Properties only.
    • null_unspecified // Default.

    View Slide

  15. Nullability
    Before
    // Objective-C
    @interface XYZList : NSObject
    - (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 }
    }

    View Slide

  16. Nullability
    After
    // Objective-C
    @interface XYZList : NSObject
    - (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 }
    }

    View Slide

  17. Swift -> Objective-C

    View Slide

  18. What can be bridged?
    • Classes

    View Slide

  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

    View Slide

  20. What can be bridged?
    • Classes
    • Protocols

    View Slide

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

    View Slide

  22. What can be bridged?
    • Classes
    • Protocols
    • Methods

    View Slide

  23. Methods
    func genericFunction(param: T) // Not bridged
    func defaultParameterFunction(param: String = "Hi") // Not bridged
    func function(param: String)
    - (void)function:(NSString * __nonnull)param;

    View Slide

  24. What can be bridged?
    • Classes
    • Protocols
    • Methods
    • Properties

    View Slide

  25. Properties
    // Swift
    let forname: String
    var surname: String
    // Objective-C
    @property (nonatomic, readonly, copy) NSString * __nonnull forname;
    @property (nonatomic, copy) NSString * __nonnull surname;

    View Slide

  26. Properties
    KVO (Key Value Observing)
    dynamic var name: String
    object.addObserver(self,
    forKeyPath: "name",
    options: .New,
    context: &myContext)

    View Slide

  27. What can be bridged?
    • Classes
    • Protocols
    • Methods
    • Properties
    • Enums

    View Slide

  28. Enums
    @objc enum MyEnum : Int {
    case SomeValue = 0
    case AnotherValue = 1
    }
    typedef NS_ENUM(NSInteger, MyEnum) {
    MyEnumSomeValue = 0,
    MyEnumAnotherValue = 1
    };

    View Slide

  29. Bridging limitations !!!
    No:
    - Structs
    - Functions
    - Operators
    - Generics
    - Subclassing Swift classes in objective-c

    View Slide

  30. Roundup

    View Slide

  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

    View Slide

  32. Questions?
    Twitter: @DanToml
    Email: [email protected]

    View Slide