What’s New in Swift 3?

This September, Apple officially released the latest version of their new programming language, Swift 3. Just like Swift 2 last year, this version contains a lot of new features and improvements, which make programming with Swift even better!

What’s New in Swift 3?

Swift 3 is included with Xcode since version 8.0 and can also be downloaded from the swift.org website.

1. Using Swift 3

If you want to use Swift 3 in your current or future projects, Xcode provides an excellent migration tool to let you easily do so.

For new Swift projects created with Xcode 8, Swift 3 will be used automatically. Upon opening a Swift 1 or 2 project in Xcode 8 for the first time, you will be presented with an alert to convert your code to either Swift 3 or Swift 2.3 code.

Choosing Swift 2.3 will give you access to all the new iOS 10, macOS Sierra, tvOS 10 and watchOS 3 APIs but will still use the Swift 2 syntax. Alternatively, you can choose not to convert your code right away and continue using an older version of Swift. When you are ready, you can then go to Edit > Convert > To Current Swift Syntax… in the Xcode menu bar to convert all of your code.

The migration tool takes care of all the changes that need to be made to your code to adopt Swift 3 from Swift 2. This includes changing method and property names, changing syntax where needed, and numerous other things. Before completing the conversion, Xcode will show you the pending changes for each of your Swift code files. This means you can verify all the changes before they are made, just to be sure the migration tool isn’t introducing bugs into your project.

2. Foundation

A major part of the Swift 3 release was the new, open-source Foundation library. While experienced macOS and iOS developers would be familiar with the Foundation framework, this library brings a few new features with it for Swift programmers.

The first immediate change you will notice when using the new Foundation APIs is the lack of an NS prefix. This means that classes such as NSDate, NSTimer and NSURL are now just Date, Timer and URL in Swift code. The code migration tool in Xcode 8 will automatically rename all of these classes for you, but this is a change which all Swift developers need to be aware of when writing new code.

Next, being a core library of Swift, all of the Foundation APIs are now compatible with Linux-based operating systems. As Swift is made compatible with other platforms, so too will the Foundation library. This means that the core set of functionality provided by Foundation, such as date/time management and networking, can be used anywhere Swift can be used. This cross-platform interoperability was one of the main motivations behind removing the NS prefix from Foundation APIs.

Lastly, many of the Foundation APIs have now been redesigned to fit in better with Swift code. For example, depending on the API, this redesign means Foundation can now take much better advantage of:

  • The improved error handling using try and catch blocks that were introduced with Swift 2.
  • Optional types, in contrast to all the implicitly unwrapped optional types which were created when importing the Objective-C Foundation framework into Swift.
  • More modern and optimised algorithms.
  • Better overall performance from being written in pure Swift, which itself is highly optimised.

Overall, using the new Swift Foundation library is a much better experience compared to using the old Foundation framework in your own Swift code. If you want to find out more about the open-source Swift Foundation library, you can check it out on GitHub.

3. API Naming Guidelines

Swift 3 also introduces some new API design guidelines. These were created in an effort to simplify all Swift APIs in addition to making them consistent.

Omit Unnecessary Words

The first change in Swift 3 to make better APIs was removing needless words from method and property declarations. This change acts both as a language feature when importing Objective-C APIs and also affects the guidelines for developers writing their own Swift code. The following code contains some examples of Swift 2 code compared to the functionally identical Swift 3 code.

let color = UIColor.redColor() // Swift 2
let color = UIColor.red // Swift 3

"Hello World!".capitalizedString // Swift 2
"Hello World!".capitalized // Swift 3

// Swift 2
if someString.containsString("a") {
    
}

// Swift 3
if someString.contains("a") {
    
}

You will see that the Swift 3 versions are more readable. The if statements in particular have a better flow and look more like an English sentence would.

Function Parameter Labels

Swift 3 actually undoes one of the changes made in Swift 2: the labeling of the first parameter in a function. In Swift 2, the label of the first parameter was automatically ignored when calling the function. For example, a function declared as:

func addObject(object: AnyObject)

would be called in Swift 2 by the following:

addObject(someObject)

Swift 3 reverses this change and no longer ignores the first parameter label automatically. This means that the above function would be called with the following:

addObject(object: someObject)

This function use, however, now goes against the omit unnecessary words goal of Swift 3. To follow the new Swift API design guidelines, the function declaration should be changed to:

func add(object: AnyObject)

This new declaration would then be used with the following code:

add(object: someObject)

This new format of function naming flows as well as the Swift 2 format but has the additional benefit of providing a label that is clearly linked to the first parameter of the function.

Please note that you can still specify that the first parameter label your Swift 3 function should be ignored. This is achieved by using a _ and then a parameter name to use within the function definition, for example:

// Definition
func add(_ object: AnyObject) {
    // Access parameter through `object` variables   
}

// Usage
add(someObject)

While this was a very basic example, this new format of declaring functions becomes extremely beneficial with more complicated names. For example, the following code shows how some of the Swift String methods have now changed from Swift 2 to Swift 3.

let replaced = someString.stringByReplacingOccurencesOfString("a", withString: "A") // Swift 2
let replaced = someString.replacingOccurences(of: "a", with: "A") // Swift 3

let array = listString.componentsSeparatedByString(",") // Swift 2
let array = listString.components(separatedBy: ",") // Swift 3

Lower Camel Case for Enums and Properties

This change aims to bring consistency across all properties in Swift 3 as well as enumerations. It was decided that, as enumeration values are closer to properties than classes, they should follow the same naming format.

This is a relatively minor change and one which the Xcode migration tool will take care of completely when converting Swift 2 code. It is something that all developers should be aware of, though, when writing new code.

The following are some examples of how this change affects particular APIs.

let color = UIColor.redColor().CGColor // Swift 2
let color = UIColor.red.cgColor // Swift 3

let alignment = NSTextAlignment.Center // Swift 2
let alignment = NSTextAlignment.center // Swift 3

4. Grand Central Dispatch

Alongside Swift 3, the open-source Swift version of the Grand Central Dispatch (GCD) core library was also released. GCD is Apple’s framework for enabling concurrent code execution on multicore systems. The main goals of the rewrite of this library are to:

  • Provide the same functionality as the existing C-based GCD functions with APIs that are designed to work and read well alongside other Swift code.
  • Provide a set of cross-platform APIs which, like the Foundation core library, will be able to be used wherever Swift is compatible.

For developers who are experienced with the GCD APIs on Apple platforms, this new Swift version will be easy to understand. It follows the same concepts. The following code shows how some common GCD tasks can now be done with Swift 3 code:

// Doing work on a background thread
let queue = DispatchQueue(label: "threadName")
queue.async {
    // Execute work
}

// Doing work on the main thread
DispatchQueue.main.async {
    // Execute work
}

// Doing work with a specified `Quality of Service`
let quality = DispatchQoS.background
queue.async(qos: quality) {
    // Execute work
}

// Doing work after an amount of time
let delay = DispatchTime.now() + .seconds(10)
DispatchQueue.main.after(when: delay) {
    // Execute work
}

If you want to learn more about concurrent programming and GCD in Swift 3, you can check out Apple’s WWDC session from this year and the Swift version of the Dispatch framework reference.

5. Benefits of Open Source

While not exactly a new feature or change, Swift 3 is the first release since Apple open sourced the programming language in December 2015. Being completely open source brings with it a lot of benefits, which will become even more evident in future versions of Swift.

Firstly, making Swift and its core libraries completely open source means that developers can port Swift to work on other platforms and operating systems. Currently Swift is compatible with all Apple platforms in addition to modern versions of Ubuntu. With time, however, Swift may be ported to other operating systems, including Android and Windows. This has the potential to make Swift a truly cross-platform language and be used more by non-Apple developers.

Next, having all of the inner workings of Swift visible to the public means that non-Apple employees can contribute to the language. In addition to new features, expert developers can also contribute towards bug fixes, improvements, and optimisations within Swift.

Overall, making Swift open source will bring improvements and features to the language in a shorter time frame since it will allow more people to contribute to it. If you want to learn more about the open-source nature of Swift, you can check out the following article:

  • What's New in Swift 3?

    Swift Is Open Source

    On December 3, Apple made a bold move, making one of the industry’s most popular and fastest growing languages open source. Find out what it means for the…

Conclusion

I’ve shown you some of the most important changes that came to the Swift language in version 3. This is an important release because, in addition to all the new features and core library APIs, it provides a clear vision of how Swift will evolve in future years. If the changes in Swift 3 are any indication, the language will only get more and more enjoyable for developers to use.

As always, please be sure to leave your feedback and comments in the comments section below. And check out some of our other courses and tutorials about the Swift language!

  • What's New in Swift 3?
    Swift
    Create iOS Apps With Swift 3
    Markus Mühlberger
  • What's New in Swift 3?
    Swift
    Create an iOS App With Swift 3: Variables, Constants, and Optionals
    Markus Mühlberger
  • What's New in Swift 3?
    Swift
    Get Started Creating an Apple TV App With Swift
    Derek Jensen
  • What's New in Swift 3?
    Swift
    How to Use Swift on Linux
    Derek Jensen