Something about Swift 2

I have to say, playground is really such a graceful way to verify my doubt during learning Swift,  all right back to record.

Name parameter function

Sometimes, ok, almost all the time, you found a function from api, and it contains 5 or 7 parameters, like this:

![](https://archive.writeitdown.site/屏幕快照 2014-11-04 下午5.12.58.png)

Well when you’re using Xcode to invoke such a function, it’s ok, cuz Xcode does a lot of  helpful hints with highlight text. But after you use fill all the parameters, it comes to :

![](https://archive.writeitdown.site/屏幕快照 2014-11-04 下午5.21.09.png)

It becomes a bit hard to recognize what those numbers and strings are. Sometimes you made a mistake between width & height (happened on me). And will become a disaster when someday you review them.

So Swift given a way to make it better, name parameter function, or we should say it has been used in OC so long.

func createATextField(#font:String,#size:Int,#width:Int,#height:Int,#text:String,#color:UIColor,#align:NSTextAlignment){

}
let textAlign = NSTextAlignment(rawValue: 1)
createATextField(font: "Hei", size: 20, width: 100, height: 30, text: "Yes!", color: UIColor(red: 0.1, green: 0.1, blue: 0.1, alpha: 1.0), align: textAlign!)

I just given a extreme example, if there is no  unmodifiable reason, never declare such a complicated function.

Tuples

It is just a handy way to declare a temporary  struct to contain more than one return values.

func searchName(#name:String)->(found:Bool,desc:String){
    return (true,"Yeah~")
}

let result = searchName(name: "Tiger")
result.found
result.desc

It supports name parameters too. also you can take any one from tuples,

let (isSucces,_) = searchName(name: "Hector")
isSucces
Optional

In my opinion, optional is just a way to avoid some unexpected errors, in action script or javascript I can write something like this:

function dodo(){
    if(isA)
    {
       return "Good";
    }
    return null;
}

and when I use it, if some  code use this result to do other things, and doesn’t do anything to check what if it’s null, it might case a crash.

Swift force us to not return nil simply, if you do it like this, you wouldn’t pass compiling.

![](https://archive.writeitdown.site/屏幕快照 2014-11-04 下午6.05.32.png)

And here comes the way called ‘Optional’, here is the code

func justRetunNil(#name:String)->String?{
    if name == "Tiger"
    {
        return "Yes, my lord!"
    }
    return nil
}
let mayBeNil = justRetunNil(name:"Tiger")
mayBeNil!

The thing we need to pay attention is, when we declare a function is optional with a question mark, the result could be nil, or a package with the result, you can’t use it directly, you need to unplug,or unpackage it with a excalmatory mark ! Swift provide a safe way to do this.

if let mayBeNil = justRetunNil(name:"Tiger"){
    print(mayBeNil)
}

Weird ,hmm. Don’t think it is clean & clear.

Type convert

Can’t stand on this, about integer, it has Int, Int16, Int32, Int64   #-_-, here is the code when I need a random index of array.

func getRandomColor() -> UIColor{
    var uintCount = UInt32(colorArray.count)
    var uintRandom = Int(arc4random_uniform(uintCount))
    return colorArray[uintRandom]
}

Missing the moments with Action Script

Shit it can be run on iOS 7!!