Swift 3.0 has been released, writes Apple engineer Ted Kremenek, bringing a wealth of changes to the language and its standard library, additions to the Linux port, and the first official release of the Swift Package Manager.
Swift 3 is Swift’s first major release since the language was open sourced by Apple at the end of 2015 and implements about 90 proposals that were discussed and approved through the Swift evolution process.
A number of changes in Swift 3.0 aim to clean up the API, as per the Swift API Design guidelines, by getting rid of a few decisions that might be seen as a legacy of Objective-C. This had the main effect of making Swift syntax shorter and more consistent:
- requiring to name the first parameter of functions and methods (unless explicitly omitted using
_
) makes it possible to write:aString.write(toFile: "filename", atomically: true, encoding: NSUTF8StringEncoding) UIFont.preferredFont(forTextStyle: UIFontTextStyleSubheadline)
aString.writeToFile("filename", atomically: true, encoding: NSUTF8StringEncoding) UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)
- omitting needless words allows developers to write
UIColor.red()
UIColor.redColor()
The two rules described above will work out an even stronger effect when taken jointly, in that, for example, the following statement:
aString.stringByReplacingOccurrencesOfString("A", withString: "a")
becomes:
aString.replacingOccurrences(of: "A", with: "a")
Another area where Swift 3 has streamlined things is in dealing with C functions belonging to several Cocoa frameworks. As an example of that, all Core Graphics functions starting with CGContext
, CGAffineTansform
, CGPDFDocument
, etc. may now be called as if they were methods of an object:
if let ctx = UIGraphicsGetCurrentContext() {
ctx.setFillColor(UIColor.red().cgColor)
...
}
CGAffineTransform(translationX: 64, y: 0)
Swift 3 also contains changes to its syntax that are are aimed to clean up the language, such as removing the ++
and --
operators, var
from function parameters, and C-style for loops with conditions and incrementers, all of which are deemed to bring more disadvantages than advantages.
The list of changes in Swift 3 is definitely too large to cover briefly. You can find all the details in the official announcement.