You probably know that Swift 3 is coming later this year. It is the first release that incorporates the hard work of a fantastic community. Dozens of proposals were submitted since Apple open sourced Swift in 2015 and dozens were accepted after careful consideration by the core team. In this article, we take a look at some of the important changes in Swift 3.
What Is Swift 3 About?
The core team has a very clear goal in mind with the release of Swift 3, creating a solid foundation for the Swift programming language. At WWDC 2016, Chris Lattner emphasized that Swift 3 introduces a range of breaking changes with the intent of getting the fundamentals right. That is the common theme of the upcoming release. This means breaking existing features, removing some features, and improving the foundation of the language.
The Swift evolution project has been a true success story for everyone involved in the Swift community. The engagement has been tremendous and the result is Swift 3. Apple is transparent about the release process and snapshots of Swift 3 are available from the Swift website and included in Xcode 8, which is in beta at the time of writing.
More Than Swift
The Swift 3 release not only focuses on the Swift programming language, it also includes substantial changes to the toolchain, the standard library, and the language's interoperability with Cocoa. Remember that Swift is more than a language. When we talk about Swift, we generally only think about the language, but it also includes the standard library and the package manager.
Source Compatibility
If you have worked with Swift, then you know that migrating a codebase from one version to another is no small feat. Unfortunately, migrating a project to Swift 3 will be no different.
That said, the primary goal of Swift 3 is to make sure the transition from Swift 3 to future versions of the language won't be like that. Source compatibility is a key focus point of Swift 3.
Awesomeness
Swift was designed to be a modern programming language, but it was equally important to create a language that looked nice and was ... well ... awesome. With Swift 3, the team continues to "optimize the language for awesomeness" as Chris Lattner puts it.
While there are many breaking changes, the net result is a language that feels and looks great. Swift 3 is a joy to use. The changes to Core Graphics and Grand Central Dispatch, which we discuss in a moment, are fine examples.
What Is Changing?
Enough about how awesome Swift is and how much more awesome Swift 3 will be. In the remainder of this article, I would like to focus on some of the key changes introduced in Swift 3. Keep in mind that Swift 3 continues to evolve until the official release later this year.
API
Readability
A lot of time and energy was spent on improving the API of the Swift language. The changes are significant, there is no denying that. But the result is very, very nice. With Swift 3, the core team aims for an API that focuses on readability and accessibility.
While many of us have become used to the verbosity of Objective-C, the new Swift API takes a different approach by only emphasizing and focusing on the essentials. Take a look at the following example of Swift 2.2.1. This example should look familiar if you have spent some time with Swift ... or Objective-C.
1
| parentViewController.presentViewController(newViewController, animated: true , completion: nil) |
In Swift 3, this code snippet looks slightly different as you can see below.
1
| parentViewController.present(newViewController, animated: true , completion: nil) |
The Swift community realized that there is no need to include a reference to what is about to be presented since that information is already included in the first parameter. As a result, the method name becomes more readable and more concise. A definite improvement if you ask me.
This is a common thread in Swift 3. Several of the proposals that were accepted and included in Swift 3 focus on simplification and removing cruft from the language. The Swift API was initially heavily influenced by the verbose nature of Objective-C. Readability is great, but Swift 3 cuts back on verbosity without compromising readability.
The Swift community is of the opinion that the design of an API should always take the use of the API into account and that is clearly visible in the changes that are introduced in Swift 3. I am sure you agree that the revamped API looks—and reads—great.
Labeling Parameters
Another important change many developers welcome is the consistent signature of function and methods by including the first parameter label by default. This is what a typical function looks like in Swift 2.2.1. By default, the first parameter label is omitted when the function is invoked.
1
2
3
4
5
| func setupView(view: UIView, withConfiguration configuration: Configuration) { // ... } setupView(view, withConfiguration: configuration) |
That is no longer true in Swift 3. The first parameter is no longer given a special treatment, which is a very welcome change.
1
2
3
4
5
| func setupView(view: UIView, withConfiguration configuration: Configuration) { // ... } setupView(view: view, withConfiguration: configuration) |
Because of this change, you could further improve the above example by omitting the reference to the view in the function name.
1
2
3
4
5
| func setup(view: UIView, withConfiguration configuration: Configuration) { // ... } setup(view: view, withConfiguration: configuration) |
Import as Member
Working with C APIs in Swift has always looked and felt clunky. Core Graphics functions, for example, are imported as global functions, which isn't a great solution and, as a result, using Core Graphics in Swift doesn't feel great.
The same applies to Grand Central Dispatch. In the next example, we use Grand Central Dispatch to asynchronously dispatch a task to a background queue.
1
2
3
| dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) { // ... } |
In Swift 3, the API feels much more like a native Swift API. Functions are imported as methods, which results in the following syntax in Swift 3.
1
2
3
| DispatchQueue.global(attributes: .qosBackground).async { // ... } |
Removing Features
The Swift community also agreed on the removal of a number of features, some of which have sparked a few heated discussions. I would like to point out four of them.
C-Style for
Loops
Does this look familiar to you?
1
2
3
| for ( var i = 0; i < 5; i++) { print(i) } |
C-style
for
loops are no longer available in Swift 3. Wait. What? Why? That is a very good question. You can read the proposal, submitted by Erica Sadun, on GitHub. This brings us to the next controversial change.
Say Goodbye to ++
and --
Soon after open sourcing Swift, Chris Lattner, the creator of Swift, submitted a proposal to remove the increment and decrement operators from the language. In his proposal, he mentions that these operators were added, "without much consideration", early in the development of Swift. To clean up the Swift API and avoid any confusion,
++
and --
are no longer available in Swift.
1
2
| var a = 0 a++ |
Don't panic, though. The solution is simple. No need to fret.
1
2
| var a = 0 a += 1 |
No More var
Parameters
If you are familiar with functions in Swift, then you know that the parameters of a function are constant by default. You can change this behavior by prepending a parameter name with the
var
keyword. For variable parameters, a variable copy of the parameter is passed to the function.
But how does this differ from parameters marked as
inout
? Right. That is exactly what many of us have been wondering and it is the motivation for removing variable parameters from the language.
From the perspective of the function, there is no difference, that is, the function receives a mutable local copy of the parameter's value. As the name implies, however, a parameter marked as
inout
writes its value back to the original variable.
To avoid any confusion,
var
parameters are no longer available in Swift 3. Fortunately, inout
parameters are here to stay.
Speaking of
inout
parameters, in Swift 3, the inout
keyword is integrated in the type syntax of function parameters. Take a look at the following examples to better understand this change.
1
2
3
4
5
6
7
8
9
| // Swift 2 func combineStrings(inout first: String, second: String) { // ... } // Swift 3 func combineStrings( first: inout String, second: String) { // ... } |
Implicit Tuple Splat Behavior
Even though Swift is still very young, there are many features that are pretty advanced. Did you know that you can pass a tuple to a function instead of a list of parameters? There is no need to cheer, though. This feature is going to be removed in Swift 3.
01
02
03
04
05
06
07
08
09
10
11
12
| // Pass Arguments As Separate Arguments let view = UIView() let configuration = Configuration() setupView(view, withConfiguration: configuration) // Pass Arguments As Part of Tuple let tuple = (view, withConfiguration: configuration) setupView(tuple) |
Chris Lattner refers to this behavior as "cute" in his proposal to remove the feature. While this behavior can be useful from time to time, it seems to have quite a few consequences. The reason for bringing up this proposal is to highlight the main goal of the core team, simplifying the syntax and the API of the language.
I can see how this feature looked neat at first, but, as the language grew, gained in complexity, and more people started using it, features like this are adding limited value to the language in exchange for what seems to be a list of complications, including performance issues during compilation and complexity to the type checker that could be avoided by omitting the feature.
What Is the Deal With Swift 2.3?
Last week, tutsplus wrote about Xcode 8. In that article, I mentioned that Xcode 8 supports both Swift 2.3 and Swift 3. But what is Swift 2.3 and how does it compare to Swift 2.2?
Swift 2.3 is a minor but important update to Swift. The main difference with Swift 2.2.1, the version included in Xcode 7.3.1, is compatibility with the SDKs for Apple's new operating systems, iOS 10, tvOS 10, watchOS 3, and macOS Sierra (10.12).
This means that you can use and build against the new SDKs without making the jump to Swift 3. With Xcode 8, you can submit applications to the App Store using Swift 2.3 or Swift 3. The Swift team knows and understands that the migration to Swift 3 has a significant impact on existing projects that include Swift. Swift 2.3 makes sure you can migrate your projects when you see fit.
Tools
What I also like about the Swift project is that the tools are developed alongside the language. This means that the tools also receive a substantial update when Swift 3 is released later this year.
Comments
Post a Comment