Technology

What’s in the New Apple Swift Developer Release

14 Mar 2015 3:26pm, by

In 2014 Apple launched Swift, a new programming language. At the time, Swift wasn’t quite complete, and despite a 1.1 release there were reports of performance issues, particularly in debug mode or with non-optimized code. There are always going to be bugs with release 1.0 of any new software, but Apple should be praised for doing a good job overall.

Swift 1.2 Released

In late February 2015 Apple launched Swift 1.2 as part of the Apple iOS 8.3 beta for registered iOS developers to test out. Apple’s policy is to do beta releases continually with their development versions to give developers a heads-up of future features and changes. After a few betas they’ll do a major release, with new full release versions of Xcode usually synced to a new iPhone/iPad version, a new version of iOS or a new gadget like Apple Watch, which will be launched in April with iOS 8.2.

So What’s New in This Developer Release?

Performance. Although if enabled, it takes a bit longer to compile. There’s a new build setting called Whole Module Optimization (see below) that optimizes all of the files in a target together. Also, the compiler now knows about incremental builds and should now only recompile files that have changed.

Xcode settings for Whole Module

The use of optionals with a non-nullable variable type, combined with the nil coalescing operator “??” lets you specify a default value if the variable is nil, and that’s been in Swift since the start. In Swift 1.2 the precedence of “??” has been raised and that impacts expressions in a good way. Try this code below in an Xcode playground. It won’t compile under Swift 1.1 but works correctly under 1.2:

The first print outputs 99999, the second 6. The slightly confusing expression to the right of the OR  || means if “a” is nil, default it to “0.” Then if a > 0 or b > 0, print the unwrapped value of the “a” optional. The nil coalescing operator “??” unwraps the value from the “a optional” but print(a) doesn’t, so it needs print(a!) to unwrap it explicitly.

The Conversion Tool

If you’ve got a lot of Swift 1.0/1.1 code, you might want to run the Swift conversion tool to advance to Swift 1.2. On the edit menu look for Convert, and on the sub menu: “To Latest Swift”. This can take a few minutes depending on how many files there are.

I tried it below on the Apple Adventure App, which came out with Swift 1.0. Before the conversion it had 103 compile errors.  Afterward the conversion it still had 26, so it’s not perfect, but it corrected 75 percent of them.

Conversion failures for Adventure

Conversion Failures for Adventure

Before you commit the conversion (by hitting save), it shows a side by side preview of the changes so you can see what will be altered, and this gives you the option of canceling the conversion. It also offers you the option of taking snapshots before mass conversions.

The problem for many of the errors was that there were two possible fixes. I just had to click on each error and select the appropriate fix, often making a type optional by appending a “?”. Others were optional types that weren’t being unwrapped.

The New Set Data Structure

Omitted from Swift 1.0, set is the equivalent of Objective-C’s NSSet type. Define a set based around a type, then add members to an instance. I used an enum type, but any type will do:

Here I’ve defined an enumeration, Colors, then created a set drinkColors based on it, initially populated with Red, Orange and Green. Then Blue is added, and Red removed twice. The unchanged third count of 3 is because you can’t remove an already removed value from a set.

Let Constants

To assign a constant value, you use the keyword let to declare it, and at the same time assign a value. Unlike many languages where you can only assign a compile time constant, Swift lets you assign a computed value. In Swift 1.2 you can now declare a constant and defer assigning a value until later. If you try to use it before assigning the value, the compiler will complain:

Unassigned error message

Below it’s used correctly:

Here it’s declared in line two, but not assigned until line three. To be honest, it’s not that big a deal. As with earlier Swift versions, this below worked:

Conclusion

Swift gets better and better with each release, and the Swift 1.2 release — even at beta — is a significant improvement. Until it’s a full release though, developers won’t be able to deploy Swift 1.2 apps in the App Store. Roll on iOS 8.3 release.

Feature image: “Swift in Flight” by aaron_eos_photography is licensed under CC BY-NC-SA 2.0.

A newsletter digest of the week’s most important stories & analyses.

View / Add Comments

Please stay on topic and be respectful of others. Review our Terms of Use.