combining swift and objective c agenda
play

COMBINING SWIFT AND OBJECTIVE-C AGENDA Using Objective-C from - PowerPoint PPT Presentation

COMBINING SWIFT AND OBJECTIVE-C AGENDA Using Objective-C from Swift Using Swift from Objective-C Objective-C Behavior in Swift Classes IMPORTING OBJECTIVE-C INTO SWIFT APPLE FRAMEWORKS No additional setup required! import UIKit import


  1. COMBINING SWIFT AND 
 OBJECTIVE-C

  2. AGENDA Using Objective-C from Swift Using Swift from Objective-C Objective-C Behavior in Swift Classes

  3. IMPORTING OBJECTIVE-C INTO SWIFT

  4. APPLE FRAMEWORKS No additional setup required! import UIKit import Foundation

  5. OBJECTIVE-C WITHIN SAME TARGET Add Objective-C files to the Bridging-Header Bridging-Header needs to be referenced in build settings Project- MyObj.m MyObj.h Bridging- Header.h Car.m Car.h A.swift B.swift

  6. THIRD PARTY FRAMEWORKS If framework is built as a module : No additional setup required import Parse

  7. THIRD PARTY FRAMEWORKS If framework is written in Obj-C And framework is not built as a module Add framework header to bridging-header: #ifndef Makestagram_Makestagram_Bridging_Header_h #define Makestagram_Makestagram_Bridging_Header_h #import <Parse/Parse.h> #endif

  8. CALLING OBJECTIVE-C FROM SWIFT

  9. CALLING OBJ-C FROM SWIFT Most syntax translates almost 1-1 Special rules for initializers

  10. INITIALIZERS [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; UITableView(frame: CGRectZero, style: .Grouped) [UIColor colorWithRed:0.5 green:0.0 blue:0.5 alpha:1.0]; UIColor(red: 0.5, green: 0.0, blue: 0.5, alpha: 1.0)

  11. METHODS [myTableView insertSubview:mySubview atIndex:2]; myTableView.insertSubview(mySubview, atIndex: 2)

  12. PROPERTIES myTextField.textColor = UIColor.darkGrayColor()

  13. OPTIONALITY IN OBJ-C Since latest release of Objective-C we can specify whether values can be nil or not For unaudited APIs: Implicitly unwrapped Optionals by default!

  14. OPTIONALITY IN OBJ-C // unaudited version -> generates warning + (UIView *)createViewWithName:(NSString *)name; // return value and argument are non-optional + (__nonnull UIView *)createViewWithNameSwiftier:(__nonnull NSString *)name; // return value and argument are optional + (__nullable UIView *)createViewWithNameSwiftierNullable:(__nullable NSString *)name;

  15. USING SWIFT FROM OBJECTIVE-C

  16. SWIFT WITHIN SAME TARGET AppName A.swift -Swift.h B.swift MyObj.m Car.m MyObj.h Car.h

  17. NSOBJECT SUBCLASSES class SimpleNSObject: NSObject { } #import "InteropTest-Swift.h" - (void)test { SimpleNSObject *simple2 = [[SimpleNSObject alloc] init]; }

  18. SWIFT ROOT CLASS @objc public class Simple { public class func newInstance() -> Simple { return Simple() } } #import "InteropTest-Swift.h" - (void)test { Simple *simple = [Simple newInstance]; }

  19. OBJECTIVE-C BEHAVIOR IN SWIFT CLASSES

  20. OBJECTIVE-C BEHAVIOR IN SWIFT CLASSES Use the dynamic keyword to enable Objective-C 
 features such as KVO and performSelector: class User { dynamic var name: String dynamic func setUp() { //... } }

  21. ADDITIONAL RESOURCES Using Swift with Cocoa and Objective-C (Apple) Mike Ash Talk: Swift and C Russ Bishop Talk: Unsafe Swift

Recommend


More recommend