sort 函数
func backward(a:Int, b:Int) -> Bool {
return a > b
}
let arr = [1,5,2,4,3,7]
var brr = arr.sort(backward)
闭包表达式语法
brr = arr.sort({ (a:Int, b:Int) -> Bool in
return a < b
})
brr.sort({a, b in return a > b})
brr.sort({a, b in a > b})
brr.sort({$0 > $1})
brr.sort(){$0 > $1}
map
var arr = [16, 58, 510]
let str = arr.map {$0 + 30}
捕获值
func a_f(a: Int) -> ()->Int {
var sum = 0
func b_f() ->Int {
sum += a
return sum
}
return b_f
}
let s1 = a_f(10)
s1() // 10
s1() // 20
let s2 = s1
s2() // 30
闭包是引用类型