闭包表达式语法
{ (parameters) -> returnType in statements }
let names = ["Chris","Alex","Ewa","Barry","Daniella"] names.sort({ (s1: String,s2: String) -> Bool in return s1 > s2 })
根据上下文推断
单表达式闭包隐式返回
let names = ["Chris",s2 in s1 > s2 })
参数名简写
运算符函数
尾随闭包
let names = ["Chris","Daniella"] names.sort(){$0 > $1}
let digitNames = [ 0: "Zero",1: "One",2: "Two",3: "Three",4: "Four",5: "Five",6: "Six",7: "Seven",8: "Eight",9: "Nine" ] let numbers = [16,58,510] let strings = numbers.map { (var number) -> String in var output = "" while number > 0 { output = digitNames[number % 10]! + output number /= 10 } return output } // strings 常量被推断为字符串类型数组,即 [String] // 其值为 ["Onesix","FiveEight","FiveOneZero"]
值捕获
func makeIncrementor(forIncrement amount: Int) -> () -> Int { var runningTotal = 0 func incrementor() -> Int { runningTotal += amount return runningTotal } return incrementor } let incrementByTen = makeIncrementor(forIncrement: 10) incrementByTen() // 返回的值为10 incrementByTen() // 返回的值为20 incrementByTen() // 返回的值为30
下面的例子中,incrementBySevne捕获了一个新的runningTotal变量,该变量和incrementByTen中捕获的变量没有任何联系:
let incrementBySeven = makeIncrementor(forIncrement: 7) incrementBySeven() // 返回的值为7 incrementByTen() // 返回的值为40
闭包是引用类型
上面的例子中,incrementBySeven和incrementByTen是常量,但是这些常量指向的闭包仍然可以增加其捕获的变量值。 这是因为函数和闭包都是引用类型。
无论您将函数/闭包赋值给一个常量还是变量,您实际上都是将常量/变量的值设置为对应函数/闭包的引用。
无论您将函数/闭包赋值给一个常量还是变量,您实际上都是将常量/变量的值设置为对应函数/闭包的引用。
let alsoIncrementByTen = incrementByTen alsoIncrementByTen() // 返回的值为50
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。