闭包有三种形式:
1、全局函数{ (parameters) -> return type in statements }
let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"] func backwards(s1 : String , s2 : String) -> Bool { return s1 > s2 } var reserve = sorted(names, backwards) println(reserve)
[Ewa, Daniella, Chris, Barry, Alex]
var reserved = sorted(names, {(s1 : String , s2 : String) -> Bool in return s1 > s2 }) println(reserved)
得到的输出为:
[Ewa, Daniella, Chris, Barry, Alex]
var reserved1 = sorted(names, {s1 , s2 in return s1 > s2}) println(reserved1)
var reserved2 = sorted(names, {(s1 : String , s2 : String) -> Bool in s1 > s2 }) println(reserved2)
let intNumbers = [2, 3, 4, 5] var reserved3 = sorted(intNumbers, { $0 > $1 }) println(reserved3)
得到的结果是:
[5, 4, 3, 2]
原文地址:http://blog.csdn.net/weasleyqi/article/details/43968859