总结:整个Closure的做用在于简化语言表述形式。git
1、闭包的简化express
Closure expression syntax has the following general form:api
Because the sorting closure is passed as an argument to a method, Swift can infer the types of its parameters and the type of the value it returns. 闭包
reversedNames = names.sorted(by: { s1, s2 in return s1 > s2 } )异步
Implicit Returns from Single-Expression Closureside
reversedNames = names.sorted(by: { s1, s2 in s1 > s2 } )函数
Shorthand Argument Namesthis
Swift automatically provides shorthand argument names to inline closures, which can be used to refer to the values of the closure’s arguments by the names $0, $1, $2, and so on.orm
reversedNames = names.sorted(by: { $0 > $1 } )ci
Operator Methods
reversedNames = names.sorted(by: >)
2、拖尾变换
闭包的实如今函数参数列表外;
再次精简
reversedNames = names.sorted() { $0 > $1 }
reversedNames = names.sorted { $0 > $1 }
彻底移出
Closures Are Reference Types
Whenever you assign a function or a closure to a constant or a variable, you are actually setting that constant or variable to be a reference to the function or closure.
Escaping Closures
异步解决方案
Autoclosures
表达式语句