首页 > 代码库 > Swift闭包(Closure)

Swift闭包(Closure)

语法: 

{ (parameters) ->return type in statements}

 实例:采用函数实现:

 

let names =["Chris", "Alex", "Ewa", "Barry", "Daniella"]
funcbackwards(s1: String, s2: String) -> Bool {
return s1 > s2
}
var reversed = sort(names, backwards)
println(reversed)

//sort 排序函数,backwards 通过排序规则的函数 采用闭包实现:
var reversed = sort(names, { (s1: String, s2: String) -> Bool in
return s1 > s2 
})
println(reversed)

Swift交流讨论论坛论坛:http://www.cocoagame.net

欢迎加入Swift技术交流群:362298485