Swift logo representing Advanced iOS App Training

Swift 2.2:Whats Changed

Alongside Xcode 7.3, Apple has released a new version of its Swift development language, called Swift 2.2

Here is a list of changes.

++ and — are now deprecated
Swift 2.2 deprecates the ++ and operators. They still work at this point but you will get a warning when you use them. These will be removed with Swift 3.0

The alternative now is to use += 1 and -= 1

C-Style for loops are deprecated
The classic c-style for loop is now deprecated. Again it works but you will get a warning. So this style of loop is now being removed.

for var i = 1; i <= 10; i += 1 {
print("A loop that repeats 10 times")
}

So the alternative you should be using is a for-in loop with a range.

for i in 1...10 {
print("A loop that repeats 10 times")
}

To make the for-in go backwards you can use the reverse() function

for i in (1...10).reverse() {
print("A loop that repeats 10 times")
}

RemoveFirst
Arrays in Swift have had the removeLast() function that removed the last item from the array and returned a new array back.

Now with Swift 2.2 Apple has added removeFirst() and, as you can guess, this removes the first item from the array and returns a new array back.

Tuples can now be compared more easily A tuple is a common separated list of values.

let character1 = ("Han" , "Solo")
let character2 = ("Luke", "Skywalker")

Prior to Swift 2.2 you could write a bunch of code to compare two tuples but with Swift 2.2 you can now compare them directly. This is a limit however, you can only compare tuples with a maxim of 6 values.

This now works

if  character1 == character2 {
print("They are the same")
} else {
print("They are different")
}

var parameters are deprecated
Arguments for functions are by default constants. Prior to Swift 2.2 you could add the keyword var to them make them variables.

This is now deprecated. If you need to change the values of the parameters just create a variable copy within your function

 Swift version checking
With Swift 2.2 you can now check the version of Swift you are using, just in case you need to support legacy versions of Swift.

#if swift(>=2.2)
print("Running Swift 2.2 or later")
#else
print("Running Swift 2.1 or earlier")
#endif

Happy coding!!

 

Leave a Reply

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