Monday, March 28, 2016

What's new in SWIFT 2.2

First and the biggest change to say so is ++ and -- deprecation 

It feels strange to say what's new in SWIFT 2.2 and jump right into deprecation:)

If you ask why? There has been a fair bit of confusion with the new generation on prefix and postfix behavior. In the past, with people from C++ experience, they know how to overload those operators and how postfix and prefix works. With the proliferation of other languages, people's experience on these intrinsic factors have gone down to say the least. There is a easy replacement with the same number of charecters += 1 or -= 1

Traditional C-style loops are gone

for(i=0;i<20 i="" p=""> //do what ever you want

}<20 i="" p="">

There are lesser & lesser situations we use something like above. SWIFT took note of it and replaced it with ranges. We can achieve the same with the code below

for i in 1...20 {

 // do something

}

What if I want to jump by an interval of 2, here's how we can do that


<20 i="" p="">for i in 1.stride(through:20, by:2) {

 // do something

}

If you need to do the reverse loop, any attempt to do the following will fail
WRONG approach
for i in 20...1 {

 // do something

}

RIGHT approach for the same is
for i in 1...20.reverse() {

 // do something

}

We more often work on arrays or collections for for loop and that remains the same
var array = Array(1...10)

for num in array {

 // do something

}



var parameters have been deprecated

This is a big change from development perspective

func printPersonInformation(var name:String, var age:Int) {

 // do something

}

We no longer need "var" against those argument variables. Now the problem with getting rid of "var" is how are we going to differentiate between const and non-const parameters. Well the argument SWIFT came up with was with "var" people got it confused with "inout". So there are give and takes with both approches. Any attempt to change name or age parameter in function body will result in a compilation error. Only way out will be to do a variable copy and proceed.

var personName = "Mr. " + name.lowerString

This may need some amount of work, given that people have been following this syntax for bit.

String selectors are deprecated

This is another big one in the block. We have all used strings as selectors in SWIFT (as opposed to @selector in objc). No escape on that now, SWIFT came around and said, no more passing string. SWIFT made it more statically linked and that's a welcome news (not the syntax though). I reckon, in the future versions there will be more changes to the syntax

SWIFT 2.0
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "More", style: .Plain, target: self, action: "moreButtonTapped:"
SWIFT 2.2
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "More", style: .Plain, target: self, action: #selector(moreButtonTapped:)

Arrays introduces removeFirst

We have always used removeLast() method to remove the last item/object from Array. Now step into removeFirst. This method allows to remove the top most or the first item from array. But remember, this will not work on optional objects, unlike removeLast(). If the collection on which removeFirst is invoked happens to be NIL, then application crash is imminent

Tuples splat syntax is deprecated

If we have a method like the following

func personInformation(name:String, age:Int) {

 // do something

}



let person = ("Leland Stottlemeyer", 35)

personInformation(person)

This way of passing tuples as argument has been deprectaed. This invocation way will be removed in subsequent releases. Talking about tuples, In SWIFT 2.2 we can now compare tuples directly

Like the following

let person1 = ("Arron Flinch", 23)

let person2 = ("Steven Smith", 24)



if person1 == person2 {

 print("Matching person")

} else {

 print("Non-mathing people, try someother person(s)")

}

Now, although this is sweet, I see what your next question is. you ask, will it work for all sizes? Answer is no, but then, SWIFT supports sufficiently good enough size, until arity 6.

Why only until 6 and not bigger than that. Well the answer is simple. If arity is more than 6, then I'd say you should re-think on the implementation and perhaps switch to "structure". What happens, when you compare tuples with different data structure, like (String, String) with (Int, String). As in the case of segment fault(s), be prepared to see some long error messages, which may or may not make much sense to you

Changes to Argument labels

You can now use a variety of language keywords, here's an example of it

func printGreeting(name: String, repeat repeatCount: Int) {

 // do something

}

As you have noticed above, "repeat" is an argument label as much as it is a SWIFT keyword. What it means is, you can use SWIFT keywords as argument labels too. That will bring a smile on everybody's face. Now, there's no reason to NOT make your swift functions more readable or read-friendly.

Renamed DEBUG identifiers

It's not just selectors have been renamed, debug identifiers have also been

__FILE__ renamed to #file

__LINE__ renamed to #line

__COLUMN__ renamed to #column

__FUNCTION__ renamed to #function


In addition, if you want to compare SWIFT versions, here's how you can do...
#if swift(>=2.2)

print("Running Swift 2.2 or later")

#else

print("Running Swift 2.1 or earlier")

#endif

Changes to documentation keywords

We know we can add comments as metadata to our code, in addition to what we had in the past, we now have 3 new additional one's "recommended, recommendedover and keyword"

Here's how we can use it
/**

Convert Kgs to lbs

- parameters:

- kgs: weight in kgs

- returns: weight in lbs

- recommended: convertWeight

- authors:

Kat Dennings

Beth Behrs

- bug: Max, I can’t believe it. I screwed me and the horse I rode in on

*/

func convertWeight(weight: Float, kgsTolbs:Bool) -> Float {

 var lbs: Float = 0.0

    //do something

    return lbs



}



var pounds = convertWeight(40.03)



/**

Convert Kgs to lbs

- parameters:

- kgs: weight in kgs

- returns: weight in lbs

- authors:

Tony Shalhoub

- recommendedover: convertWeight

- bug: It's a Jungle Out There

*/

func convertKgsToLbs(weight: Float) -> Float {

 var lbs: Float = 0.0

    //do something

    return lbs



}

As you can see, recommended lets you say "prefer this other method instead", whereas recommendedover lets you say "prefer me over this other method."

Other minor stuff

Xcode 7.3 does feature all-new code completion: you can now type something like "strapp" to have "stringByAppendingString" highlighted in the code completion, or "uitavc" to have "UITableViewCell" highlighted. It will take a little thinking to rewire your brain to use these text shortcuts, but it does promise a significant speed up for your coding. In addition you can see code completion in console window too. That's a big win from a developer stand-point.

On the downside XCode still crashes when you print certain variables in console window or attempt to debug through breakpoints on some occassions. Hope they get addressed soon enough.