Start using Swift in your Objective-C projects. Avoid some headaches with these useful tips and advices for the communication layer between your Objective-C code base and your future Swift implementations
target's public headers that you would like to expose to Swift. // // I can import CocoaPods Libraries here! #import <AFNetworking/AFNetworking.h> // And my Objective-C classes #import "CocaColaAlgorithm.h" Swift implementation // Use here your Objective-C exposed classes let cola = CocaColaAlgorithm.prepareCola()
• public elements • private are exposed if marked with @IBAction, @IBOutlet, or @objc • internal are exposed if the project has an Objective-C bridging header • Objective-C compatible features
Structures defined in Swift • Top-level functions defined in Swift • Global variables defined in Swift • Typealiases defined in Swift • Swift-style variadics • Nested types and curried functions
protocol and its type is not know until runtime) if let fifthCharacter = myObject.characterAtIndex?(5) { println("Found \(fifthCharacter) at index 5") } You can use casting if let date = lastRefreshDate as? NSDate { println("\(date.timeIntervalSinceReferenceDate)") }
adopt Swift protocol if they are NSObjectProtocol Adopting swift protocols in objective-C /** MyProtocol.swift */ @objc protocol MyProtocol: NSObjectProtocol { // Protocol stuff }
let realmAndObjc = "http://realm.io/news/swift-objc-best-friends-forever" let swiftReady = "http://www.toptal.com/swift/swift-is-it-ready-for-prime-time" let swiftImprovesObjc = "http://spin.atomicobject.com/2014/06/13/swift-improves-objective-c/" //MARK: - Slides let slidesURL = "http://bit.ly/1AaKRfv" println("The slides are available here: \(slidesURL)") //MARK: - Contact let emailURL = NSURL(string: "mailto://[email protected]")! UIApplication.sharedApplication().openURL()