Swift2

======================New features on Swift2===========================

1) Checking OS support for a specific API (When you're using an API function/class... how to check if it's supported on IOS8 or ios7, ...)


Suppose we want to use NSDocument class somewhere in our codes ,in the good old time we did that:


if NSClassFromString("NSDocument") != nil {

    // iOS 8 or up

} else{

    // Earlier iOS versions

}

Now you cand simply do that:


if #available(iOS 8, *){// iOS 8 or up


    // iOS 8 or up

    let doc = NSDocument()

    

} else {

    // Earlier iOS versions

    

}

-------------------------------------------------------------------------

2)Protocol Extension:

Now in Swift2 you can add extend functionality of a protocol like we did for class/structures/enum...

by Extension


protocol Greeting {

func welcome() ->Void

}

class Politeness:Greeting {


func welcome() ->Void{

print("Hello , welcome")

}


}


let pol = Politeness()

pol.welcome()  // print "Hello , welcome"


add extension:


Extension Greeting {


func warmWelcome()->(){

print("Hi darling , welcome! happy to see you again")

}

}

pol.warmWelcome()//print "Hi darling , welcome! happy to see you again"

--------------------------------------------------------------------------

3) do..while loop replaced by repeat..while


var i=0

repeat{

i++

print(i)

}while(i<10)

----------------------------------------------------

4) Introduction of great guard/else word key

guard/else is a new pattern for condition checking syntax is :


guard A else{

//do something if condition not meet

return

}

//do something on A if everything is good!


In fact guard let you focus on only objects/data who are safe and passed the checking!

there are two advantage on this :

a-function body are much more clean and easy to track errors!

b-optional functions arguments are automatically unwrapped after condition meet. example


func useTraditionalIf(x: Int?) {

    if let x = x where x > 0 {

        // Do stuff with x

        x.description

    }

    

    // Value requirements not met, do something

}


func useGuard(x: Int?) {

    guard let x = x where x > 0 else {

        // Value requirements not met, do something

        return

    }

    

    // Do stuff with x

    x.description

}


guard has a similar behaviour with assert()

-------------------------------------------------------------

5) println() replaced by print() 


to have the line breaking like before use appendNewline boolean flag,so equivalent of old println becomes

print("Engine started", appendNewline: true)


Protocol Extension:


In Swift 2 , protocol can be extended by Extension as we have seen in fundamental base class

below we intruduce the concept by a very simple example

let’s start by declaration of a basic protocol ,suppose:

protocol BasicAdder{

    func display()

    func calculate()

    

}



class Adder: BasicAdder{

    var num1:Int

    var num2:Int

    var result:Int!

    init(number1:Int,number2:Int){

    num1 = number1

    num2 = number2

    

    }

    func display(){

        print("\(result)")

        

    }

    func calculate() {

        result = num1+num2

    }

}

let adder = Adder(number1: 5,number2: 8)

adder.calculate()

adder.display()


Now let’s make an extension on our protocol by adding a multiplyer function

extension BasicAdder{

    func multiply(number1:Int,number2:Int)->Int{

        return number1*number2

    }

}

Now without any change to our class we can call multiply

let result2 = adder.multiply(5, number2: 8)


===========================================================================================================


http://stackoverflow.com/questions/30791488/swift-2-guard-keyword

http://www.appcoda.com/swift-2-introduction/

 © Xosrov 2016