@noescape @autoclosure @autoclosure(escaping)
//closures 闭包
let names = ["Chris","Alex","Eva","Barry","Dannis"]
func backward(s1 : String , _ s2:String) ->Bool{
return s1 > s2
}
var reversed = names.sort(backward)//从高到底 ["Eva", "Dannis", "Chris", "Barry", "Alex"] e d c b a
reversed = names.sort( { (s1: String, s2: String) -> Bool in return s1 > s2 } ) //in 前面的是申明 后面的是函数体
reversed = names.sort( { s1, s2 in return s1 > s2 } ) //omit 参数类型
reversed = names.sort( { s1, s2 in s1 > s2 } ) //omit return
reversed = names.sort({ $0 > $1 }) //简写 省略参数的定义 省略in
reversed = names.sort(>)//["Eva", "Dannis", "Chris", "Barry", "Alex"]
//trailing closures
func someFunctionThatTakesAcloure(closure :() -> Void){
//function body
}
someFunctionThatTakesAcloure({
//closure body
})
someFunctionThatTakesAcloure(){
//training closure's body
}
reversed = names.sort(){
$0 > $1
}
reversed = names.sort {
$0 > $1
}
//map
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{
(number) -> String in
var number = number
var output = ""
while number > 0{
output = digitNames[number % 10]! + output
number /= 10
}
return output
// its value is ["OneSix", "FiveEight", "FiveOneZero"]
}
16 % 10
16 / 10
func makeIncrementer(forIncrement amount: Int) ->() ->Int{
var runningTotal = 0
func incrementer() -> Int{
runningTotal += amount
return runningTotal
}
return incrementer
}
makeIncrementer(forIncrement: 3)
let incrementByTen = makeIncrementer(forIncrement: 10)
incrementByTen()//10
incrementByTen()//20
//Nonescaping cloures //@noescape 声明函数包 不可以逃 能被@noescape的是As an example, the sort(_:) method takes a closure as its parameter, which is used to compare elements. The parameter is marked @noescape because it is guaranteed not to be needed after sorting is complete.
func someFunctionWithNonescapingClosure(@noescape closure:() ->Void){
closure()
}
//这里的参数不可以使用 @noescape 他是函数外面声明的一个变量不可以使用@noescape 也就是说直到外部函数执行完成之后才会回调的
var completionHandlers:[() -> Void] = []
func someFunctionWithEscapingClousure(completionHandler:() ->Void){
completionHandlers.append(completionHandler)
}
//@noescape 标志闭包 和self的作用类似
class SomeClass {
var x = 10
func doSomething() {
someFunctionWithEscapingClousure{ self.x = 100 }
someFunctionWithNonescapingClosure {x = 200 }
}
}
let instance = SomeClass()
instance.doSomething()
print(instance.x)
// Prints "200"
completionHandlers.first?()
print(instance.x)
// Prints "100"
//Autocllsures : the array element isn’t removed until the closure is actually called
var customersInLine = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]
print(customersInLine.count)
// Prints "5"
let customerProvider = { customersInLine.removeAtIndex(0) }
customersInLine //["Chris", "Alex", "Ewa", "Barry", "Daniella"]
print(customersInLine.count)
// Prints "5"
print("Now serving \(customerProvider())!")
// Prints "Now serving Chris!"
print(customersInLine.count)
// Prints "4"
//@autoclosure 使用@autoclosure标志
func serveCustomer(@autoclosure customerProvider: () -> String) {
print("Now serving \(customerProvider())!")
}
customersInLine //["Alex", "Ewa", "Barry", "Daniella"]
serveCustomer(customersInLine.removeAtIndex(0))
customersInLine //["Ewa", "Barry", "Daniella"]
customersInLine.removeAtIndex(0) //使用@aotolosure 可以使string类型转为closure
// Prints "Now serving Ewa!"
customersInLine //["Barry", "Daniella"]
// @autoclosure(escaping)
var customerProviders: [() -> String] = [] //[]
func collectCustomerProviders(@autoclosure(escaping) customerProvider: () -> String) {
// print("Now serving \(customerProvider())!") 只有调用了customerProvider() 才会执行customersInLine.removeAtIndex(0)
customerProviders.append(customerProvider)
}
customersInLine //["Barry", "Daniella"]
collectCustomerProviders(customersInLine.removeAtIndex(0))
collectCustomerProviders(customersInLine.removeAtIndex(0))
customersInLine //["Barry", "Daniella"]
customerProviders //[() -> String, () -> String]
print("Collected \(customerProviders.count) closures.")
// Prints "Collected 2 closures."
for customerProvider in customerProviders {
print("Now serving \(customerProvider())!")
}
customersInLine //[]
// Prints "Now serving Barry!"
// Prints "Now serving Daniella!"