Moving to Swift 3.0

| | 0 Comments| 9:21 AM

Swift 3.0

Hey! Interested in an apt-get repository for Swift 3.0? We’ve got packages for Xenial, Wily, and Trusty.

Swift 3.0 is introducing a number of changes, the least of which is moving to conformance of the ratified API naming conventions. If you read through the naming conventions you’ll see that the overarching theme is one of streamlining and simplification.

Let’s take a look at a few examples of this streamlining with this 2.2 script swiftcat.swift:

Swift 3.0 changes the (count:repeatedValue) function signature for array initialization to (repeating:count:). So rather than var buf = [CChar](count:BUFSIZE, repeatedValue:CChar(0)) we will use var buf = [CChar](repeating:0, count:BUFSIZE) (note that we also simplified CChar(0) to just 0).

The fromCString String method has been removed in favor of an initializer in the form of init(cString:). As an initializer it also guarantees an object will be returned, so there’s no need for using the !: print (String(cString:buf), terminator:"")

And finally, CommandLine is used instead of Process. Our new Swift 3.0 version of swiftcat.swift:

Let’s take a look at another change in Swift 3.0 (which I appreciate), and that’s the named parameter is now required for the first argument to a function. For example, in Swift 2.2 the function func translate(text:String, from:String, to:String) would be invoked with something like translate("Hello world", from:"en", to:"es"). In Swift 3.0 the text parameter label is required: translate(text:"Hello world", from:"en", to:"es").

A few more examples of changes made:

Regardless of what you think about the wisdom of this String extension, this is functional Swift 2.2 code:

The Swift 3.0 complains that value of type 'Index' (aka 'String.CharacterView.Index') has no member 'advancedBy'. That’s because the function has changed from advancedBy(_:Index) to advanced(by:Index) (and somewhere buried in the API guidelines is a rationale as to the change).

Our next error is init(start:end:) is unavailable: use the '..<' operator. I must admit that the ..< operator looks weird (there, I said it), but okay, we’ll play along and update the code to return self[Range(startIndex..<endIndex)].

In-N-Out (Burger)

The placement of the inout keyword has also changed. In Swift 2.2 our function for emulating certain ncurses calls looked like this:

That resulted in error: 'inout' before a parameter name is not allowed, place it before the parameter type instead, so now we do this:

Great Renaming

As a part of the Great Renaming the NS prefix has been dropped from much of the Foundation classes. NSThread is now Thread, NSDate is now Date. You get the idea.

In addition to renaming classes, there has been a significant number of API changes centered around simplifying things. A great example of this simplification can be found in formatting dates and creating dates from strings. See our post on handling dates for more examples.


Using NSNotificationCenter has also changed between Swift 2.x and Swift 3.0. As a part of the Great Renaming the notification center is now simply named NotificationCenter. For a complete writeup on notifications see our Notifications and userinfo with Swift 3.0 post.

But Wait!

Is that all there is to Swift 3.0 you ask? Of course not! The Swift Evolution repository has a complete list of all of the various changes. Have a look!

Get Started With Swift 3.0

There are a number of ways to start playing with Swift 3.0 today. Apple releases snapshot builds every so often on the official Download Swift page. Make sure and grab one of the Trunk Development (master) snapshots. The snapshots are in the form of .tar.gz files that can be extracted on your filesystem.

If you’re running Ubuntu 14.04 or Ubuntu 15.10 and like the convenience of installing software with apt-get, you can also try out our Swift 3.0 Ubuntu Packages.

Finally, get some practice. There’s some code in our moreswift Github repository that is all written in Swift 2.2. As an exercise download this code and try running with a Swift 3.0 compiler. There’s no telling what you’ll run into; if you find something that isn’t addressed here drop us a line so we can update the post.

Leave a Reply

Your email address will not be published. Required fields are marked *