Swift 3.0 学习笔记之Closures(闭包 , Block)

定义一个数组,并排序

let names= ["Chris","Alex","Ewa","Barry","Daniella"]

func backward( _ s1 : String, _ s2 : String) ->Bool{

         return s1>s2

}

var reversedNames=names.sorted(by:backward)  // 数组自带的排序方法 (排序方式使用自定义的函数backward)

// reversedNames is equal to ["Ewa", "Daniella", "Chris", "Barry", "Alex"]

闭包其实就是没有函数名的方法

{ (parameters) -> return type in

         statements

}

reversedNames=names.sorted(by: { (s1:String,s2:String) ->Boolin

          returns1>s2

})

Inferring Type From Context // 从上下文推断类型

reversedNames = names.sorted(by: { s1, s2 in return s1 > s2 } )

Shorthand Argument Names // 缩写参数名

reversedNames = names.sorted(by: {$0>$1} )

Operator Methods // 操作符的方法

reversedNames=names.sorted(by: >)

Trailing Closures // 闭包作为函数参数  尾闭包

func someFunctionThatTakesAClosure(closure: () ->Void) {

        // function body goes here

}

// Here's how you call this function without using a trailing closure:

someFunctionThatTakesAClosure(closure: {

       // closure's body goes here

})

// Here's how you call this function with a trailing closure instead:

someFunctionThatTakesAClosure() { // 和上面的作用相同  将函数的闭包参数单独拎出来放在末尾

         // trailing closure's body goes here

}

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 = ""

   repeat{

        output = digitNames[number%10]!  + output

        number /= 10

   }while number > 0

   return output

}

// strings is inferred to be of type [String]

// its value is ["OneSix", "FiveEight", "FiveOneZero"]

闭包可以从定义它的周围环境捕获常量和变量。 然后,闭包可以引用并修改那些常量和变量的值,即使定义常量和变量的原始作用域不再存在。

func makeIncrementer(forIncrement   amount:Int) -> () ->Int{  // 这个方法返回一个函数

        var runningTotal=0

       func incrementer() ->Int{

               runningTotal +=amount

               return  runningTotal

       }

       return  incrementer

}

let incrementByTen = makeIncrementer(forIncrement:10) // 参数amount=10  incrementByTen 这个是 incrementer函数的函数类型    incrementByTen现在就是指向 incrementer 这个函数 ,并且 incrementer函数返回值runningTotal = 0

incrementByTen()  //相当于调用了incrementer()函数 而且amount=10 runningTotal=10

// returns a value of 10

incrementByTen()  //相当于调用了incrementer()函数 而且amount=10 runningTotal=20

// returns a value of 20

incrementByTen()  //相当于调用了incrementer()函数 而且amount=10 runningTotal=30

// returns a value of 30

let incrementBySeven = makeIncrementer(forIncrement:7)

incrementBySeven()

// returns a value of 7

incrementByTen()

// returns a value of 40

Closures Are Reference Types  //闭包是引用类型

Escaping Closures  //当闭包作为参数传递给函数时,闭包被称为逃逸函数,但在函数返回后被调用。

var   completionHandlers: [() ->Void] = []

func  someFunctionWithEscapingClosure(completionHandler: @escaping() ->Void) {

       completionHandlers.append(completionHandler)

}

func someFunctionWithNonescapingClosure(closure: () ->Void) {

        closure()

}

class SomeClass{

       var x=10

       func doSomething() {

               someFunctionWithEscapingClosure{self.x=100}

               someFunctionWithNonescapingClosure{x=200}

       } 

}

let instance=SomeClass()

instance.doSomething()

print(instance.x)

// Prints "200"

completionHandlers.first?()

print(instance.x)

// Prints "100"

你可能感兴趣的:(Swift 3.0 学习笔记之Closures(闭包 , Block))