It all started with this proposal on Swift Evolution:
“Better Translation of Objective-C APIs into Swift”.
When Swift was first announced, one of the huge selling points was how we could use the same Cocoa APIs we were already familiar with.
Unfortunately, this meant a lot of Objective-C baggage found its way into the Swift code we were writing.
Swift 3 aims to fix this by improving the way Swift imports Objective-C code.
Take this Swift 2 code:
The function is imported with a definition like this:
func addLineToPoint(_: CGPoint)
In Swift 3, this would instead be imported as:
func addLine(to point: CGPoint)
And we could use it like this:
Swift 3 removes the repetitive names of the types, and properly brings in a (now required) argument label for the first parameter.
Additionally, Swift 3 drops the NS prefix from Foundation types, so instead of:
var now = NSDate()
We’ll now be writing:
var now = Date()
Many Foundation types also now get proper Swift let/var mutability semantics:
var now = Date() now.addTimeInterval(60) // works let now = Date() now.addTimeInterval(60) // compiler error
Swift 3 also prepends the word ‘is’ to imported Objective-C Boolean properties, and converts enums to be lowercased:
path.isEmpty // was just path.empty in Swift 2
var formatter = NumberFormatter() formatter.numberStyle = .spellOut
That’s all for now, next time we’ll continue looking at more Swift 3 changes and enhancements.