What’s new in Swift 2.2?
Swift 2.2 is going to be released alongside Xcode 7.3 and is supposed to be sometime in March to May of 2016. This issue is planned to be an intermediate point between Swift 2 and Swift 3 contains more changes. Actually Swift 2.2 only warns you about changes, when Swift 3 would not allow you many things you have got used to. But do not worry – I guess it is another big step in making Swift swifter.
I am going to point out a few most important changes coming to Swift. Right now you can check it out yourself by downloading Swift 2.2 snapshot which works with Xcode 7.2 and 7.3 beta with a little bit of magic. Everything you need to be familiar with recent Swift version you can find on the official website.
Operators
Swift 2.2 gets rid of ++
and --
operators. At first glance I hated that, but in fact it is usually unnecessary to use it in modern Swift. New for
loop and higher order functions like map
or filter
made incrementation unneeded. Of course you can still increment on your own, but in different way.
1 2 3 4 5 6 7 8 9 10 11 12 | func plusPlus() { var i = 0 i++ // WARNING: '++' is deprecated: it will be removed in Swift 3 i += 1 // '+=' and '-=' goes for integer or floating point types // succesor() and predecessor() goes for Index types let alphabet = "abcdefghijklmnopqrstuvwxyz" let index = alphabet.characters.indexOf("u") if let index = index { alphabet.characters[index.successor()] } } |
According to Chris Lattner these increment/decrement operators are confusing, especially if they exists in two forms: as prefix and postfix. In fact I do not remember the last time I had to use ++x
, so removing them could make Swift easier to learn as first programming language.
C-style loop
These legacy loops are another carry-over from C. Removing for init; comparison; increment {}
lets also remove ++
and --
easily. But do not worry – Swift has really useful pretty for-in
loop.
1 2 3 4 5 6 7 8 9 10 11 12 | func forLoop() { // WARNING: C-style for statement is deprecated // and will be removed in a future version of Swift for var i = 1; i <= 10; i += 1 { print("I'm number \(i)") } // Brand new swift style works well for i in 1...10 { print("I'm number \(i)") } } |
Slices
Wow, I have waited for this for ages. In Swift 2.2 arrays have removeFirst()
function. It was easy to make it on your own as extension, but now it is in the box. The function is pretty clever and even if your array can be filled with optionals, it just removes first element, not keep it as nil
. Love it.
1 2 3 4 5 | func slices() { var array = ["First", "Second", "Third", "Fourth"] array.removeLast() // it's been here for ages, but… array.removeFirst() // is brand new (each n element becomes n-1 element) } |
Tuple comparision
Maybe it is not a breaking news, but weird it was missing. Since Swift 2.2 it is possible to compare tuples containing up to 6 Equatable
items.
1 2 3 4 5 6 | func tuples() { let tuple1 = (1, true, 3, 4, 5, "a") let tuple2 = (1, false, 3, 4, 5, "a") let equalTuples = tuple1 == tuple2 print(equalTuples) // false } |
No more currying
It can break your heart if you actually love curried functions, but I do not. They are reimagined in Swift 2.2 and becomes a little bit more readable.
1 2 3 4 5 6 7 8 9 10 11 | // WARNING: Curried function declaration syntax will be removed // in a future version of Swift; use a single parameter list func noMoreCurryingThisWay(a: Int)(b: Int) -> Int { return a + b } func curryThisWay(a: Int) -> (Int) -> Int { return { (b: Int) -> Int in return a + b } } |
Check Swift Version
Pretty interesting, but I hope it would not be needed so much. I prefer to refactor my Swift 2 compatible code to Swift 2.2 than supporting both versions (or more). But in some cases it could be useful (i.e. framework that should work with both Swift 2.0 and 2.2 – or even 3.0). Check it out.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | func swiftVer() { var cuteNumber = 7 #if swift(>=2.2) print("Good morning, I'm brand new Swift") cuteNumber += 1 #else print("Hi, I'm elder than my brother, but can ++") cuteNumber++ #endif // Such a shame '#if swift' looks like retro-Obj-C macro, // and not like swift '#available'. Take a look at this hash '#' prefix. // Looks old, and I would love to see it this way: /* if #swift(>=2.2) { } else { } */ } |
Selector
Another macro-style which I do not prefer. Swift 2.2 does not let you specify selector as string itself. In the public review #selector
has been chosen rather than Selector()
which looks like instance construction (and is also deprecated). In fact it is still better than just string thanks to autocompletion; chances for a typo are minor.
1 2 3 4 5 6 7 8 9 | func selector() { let button = UIButton() // WARNING: Use of string literal for Objective-C selectors // is deprecated; use '#selector' instead button.addTarget(self, action: "forLoop", forControlEvents: .TouchUpInside) // In fact '#selector' works well button.addTarget(self, action: #selector(forLoop), forControlEvents: .TouchUpInside) } |
Conclusion
That is not all the changes coming to Swift 2.2, but in my personal opinion most important. From this point I can recommend you Swift’s official changelog (but in fact it is more like a roadmap) and swift-evolution on Apple’s GitHub with all proposals for this evolving language.
I really like where we are now and appreciate coming changes. Swift is modern and brave. You have got to be modern if you want to outgrow Obj-C. You have got to be brave if you want to remove C-style loop being part of this language since 1972. Fingers crossed!
About the author
Ready to take your business to the next level with a digital product?
We'll be with you every step of the way, from idea to launch and beyond!
Thank u , Very good article
Wow you don’t know the difference between prefix and postfix “++” – you must be new to coding. And Apple sucks too.
prefix “++” is to never create a temp var to keep previous value. It is more efficient than postfix.
I do believe I’ve never wrote that I didn’t know the difference between the two. I simply stated that they can be confusing to newcomer programmers. Thank you for your opinion and take care.