Alex headshot

AlBlue’s Blog

Macs, Modularity and More

Swift 2.2 Released

2016, book, swift

Today, Apple released Swift 2.2 for OSX and Linux, which is the first release since the project was open-sourced last year. It contains a number of contributions from non-Apple employees as well as a number of source-incompatible changes.

To celebrate the release, until the end of March, my Swift Essentials book has a 50% off discount for the eBook and a 30% off discount for the printed book. More information is available on the Swift Essentials website.

The changes for Swift 2.2 include:

  • The removal of the C-style for loop (SE-0007)
  • The removal of increment ++ and decrement -- operators (SE-0004)
  • The replacement of typealias with associatedtype as a keyword (SE-0011)
  • Using #selector(foo) instead of Selector("foo") (which is now compile checked) to fit in better with # designated operators in Swift (SE-0022)
  • The addition of #if as a build-time and operating-system time test (SE-0020)

Any code that has:

For loops - the old way
1
2
3
for(var i=0; i<10; i++) {
  ...
}

needs to be refactored to:

For loops - the new way
1
2
3
for i in 0..10 {
  ...
}

Any references to:

Selectors - the old way
1
let s = Selector("doStuff")

needs to be refactored to:

Selectors - the new way
1
let s = #selector(doStuff)

Using the Swift version syntax allows for different paths to be taken by the compiler in the code:

Building platform-specific code
1
2
3
4
5
6
7
8
9
10
11
#if os(Linux)
import Glibc
#else
import Darwin
#endif

#if arch(i386)
let bits = 32
#elseif arch(x86_64)
let bits = 64
#endif

and can be used to selectively enable new features based on the release:

Building version-sensitive code
1
2
3
4
5
6
7
#if swift(>=3.0)
 print("Welcome to Swift 3")
#elseif swift (>=2.2)
 print("Welcome to Swift 2.2")
#else
 print("Please upgrade your version of Swift")
#endif

Fix-its built into Xcode 7.3 (which was released at the same time) will allow you to migrate code from one version to another.

Xcode 7.3 is avaliable from Apple for OSX, and Swift 2.2 is available from the Swift website for OSX and Ubuntu.