📜 ⬆️ ⬇️

Swift 2.2. Major innovations

Hello, dear readers!

Thank you for practically daring off the shelves our experimental book on the Swift language. We are seriously considering a reprint in April, we are consulting with the author about updates, as well as looking for other, more fundamental books. In the course of these searches, I had a chance to get acquainted with an excellent article that could be called “What the Swift is coming to prepare for us.” Its translation will be found under the cut.

Enjoy reading!

')
The release of Swift 2.2 is scheduled with the release of Xcode 7.3 and should take place between March and May 2016. This version is positioned as an intermediate between Swift 2 and Swift 3 and involves a lot of changes. In principle, Swift 2.2 simply warns you about changes, whereas Swift 3 will not allow you to do many things you are used to. But do not worry - I think this is another important step that will help make Swift even more sweaty.

Here I will mention a few of the major changes planned in Swift. You can try them out right now by downloading the demo version of Swift 2.2, which will work with Xcode 7.2 and 7.3 beta, if you poke a little. Previously you just need to get acquainted with the latest version of Swift, described on the official site .

Operators

In Swift 2.2 there will be no ++ operators and -- . At first glance, this angered me, but, in fact, in modern Swift, they are simply not needed. The need for increments is eliminated thanks to a new for loop and higher order functions like map or filter . Of course, you can still implement the increment yourself, but in a different way.

 func plusPlus() { var i = 0 i++ // : '++' :  Swift 3     i += 1 // '+='  '-='          // succesor()  predecessor()     let alphabet = "abcdefghijklmnopqrstuvwxyz" let index = alphabet.characters.indexOf("u") if let index = index { alphabet.characters[index.successor()] } } 


Chris Lattner believes that these increment / decrement operators only confuse, especially if they exist in two forms at once: prefix and postfix. In fact, I don’t remember when I last had to use ++ x, so if you get rid of them, Swift may be easier to learn as the first programming language.

C-like cycles

Such inherited loops are another thing borrowed from C. Removing for init; comparison; increment {} for init; comparison; increment {} for init; comparison; increment {} , we can also easily remove ++ and -- . But do not worry - there is an excellent and very convenient for-in loop in Swift.

 func forLoop() { // : C-  for  //       Swift for var i = 1; i <= 10; i += 1 { print("I'm number \(i)") } //   Swift  : for i in 1...10 { print("I'm number \(i)") } } 


Slices

This is what I have been waiting for ages. Arrays in Swift 2.2 have the removeFirst()
function removeFirst()
. You could easily write it yourself as an extension, but now it is provided out of the box. The function is very smart, and, even though your array may be filled with options, it simply deletes the first element, and does not leave it as nil
nil
. Love.

 func slices() { var array = ["First", "Second", "Third", "Fourth"] array.removeLast() //   , … array.removeFirst() //   ( n-   n-1) } 


Tuple comparison

It may not be a sensation, but it is strange that such an opportunity has not yet existed. In Swift 2.2 and later, you can compare tuples containing up to 6 Equatable
elements Equatable
Equatable
.

 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

You may be upset if you really liked the curried functions, but I was not upset. In Swift 2.2, they are rethought and become a little more readable.

 // :       //    Swift;      func noMoreCurryingThisWay(a: Int)(b: Int) -> Int { return a + b } func curryThisWay(a: Int) -> (Int) -> Int { return { (b: Int) -> Int in return a + b } } 


Swift version check

A very interesting opportunity, though I hope you don’t have to use it often. I prefer to refactor code that is compatible with Swift 2, to Swift 2.2, and not to support two (or more) versions at once. But in some cases this feature can be useful (for example, we write a framework that should work with both 2.0 and 2.2 - and, possibly, even with 3.0). Try it.

 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 //  , '#if swift'      Obj-C, //   -  '#available'.      '#'. //  ,    ,    : /* if #swift(>=2.2) { } else { } */ } 


Selector

Another macro trick I avoid. In Swift 2.2, you cannot specify a selector as a string. In the open discussion, #selector
was selected #selector
#selector
and not Selector()
Selector()
that reminds instantiation (and also undesirable). In fact, this option is still better than just a string, thanks to autocompletion; the probability of a typo is minimal.

 func selector() { let button = UIButton() // :      Objective-C // ;    '#selector' button.addTarget(self, action: "forLoop", forControlEvents: .TouchUpInside) //    '#selector'   button.addTarget(self, action: #selector(forLoop), forControlEvents: .TouchUpInside) } 


Conclusion

These are not all the changes expected in Swift 2.2, but, in my opinion, the most important. I recommend to look into the official swift journal of changes (although in reality this is just a “road map”) and to the swift-evolution section on Apple's GitHub, where all the proposals about the evolution of the language are voiced.

I am very inspired by this development of events, I look forward to the coming changes. Swift modern and cool. You have to be modern if you want to outgrow Obj-C. It takes determination to get rid of these C-like cycles that have been used since 1972. Let's hope for the best!

Source: https://habr.com/ru/post/277857/


All Articles