Aug 12, 2015
Swift-er SDK
In Xcode 6.3 we added the new nullability annotations language feature for Objective-C. This feature gave Objective-C a way to express the same sorts of null and non-null API contracts that Optionals provide in Swift. Xcode 7 continues to make communication between Objective-C and Swift more natural by introducing a lightweight generics model for Objective-C. Generics enable the two languages to safely understand and share collections containing specific kinds of elements.
These features are useful for anyone writing apps that contain both Swift and Objective-C code. But there’s a much larger collection of Objective-C code that app developers use every day: the frameworks that make up the Apple SDKs. To improve the experience of working with Swift and with Objective-C, we made a company-wide effort to provide this information in our SDK headers. In Xcode 7 you’ll find that nearly all of the common frameworks now specify the nullability of their APIs and the elements of their collection types. This takes our Swift interface from this:
class UIView : UIResponder {
init!(frame: CGRect)
var superview: UIView! { get }
var subviews: [AnyObject]! { get }
var window: UIWindow! { get }
// ...
func isDescendantOfView(view: UIView!) -> Bool
func viewWithTag(tag: Int) -> UIView!
// ...
var constraints: [AnyObject]! { get }
// ...
}
To this:
class UIView : UIResponder {
init(frame: CGRect)
var superview: UIView? { get }
var subviews: [UIView] { get }
var window: UIWindow? { get }
// ...
func isDescendantOfView(view: UIView) -> Bool
func viewWithTag(tag: Int) -> UIView?
// ...
var constraints: [NSLayoutConstraint] { get }
// ...
}
The last piece of the puzzle is the Xcode 7 tool to convert your code to Swift 2. This tool lives in Xcode under the Edit menu as Convert > To Latest Swift Syntax. The tool takes a project that uses Swift 1.2 and applies the necessary edits to turn your code into valid Swift 2 code. These changes account for the improved header information. For instance, if you are overriding a method whose parameter and result types are now more precise, the migrator will update your method to match.
The improvements to Objective-C were detailed at WWDC in Swift and Objective-C Interoperability starting at the 14:30 mark. Note that this video uses the Xcode 6.3 __nullable syntax as opposed to the updated syntax in Xcode 7 that uses _Nullable. For more information on nullability annotations, see the blog post Nullability and Objective-C. For more information on Swift 2 and Objective-C lightweight generics, see the Xcode 7 Release Notes.