Today, a friend recommended an interesting page,A domain name is like:
http://fuckingclosuresyntax.com
In the Swift,All functions are closures,Standard functions are just the most complete closures name with parameters。
All right,The following is the text
asvariable:
1 |
var 闭包名称: (参数类型) -> (返回类型) |
asOptional variable:
1 |
var closureName: ((parameterTypes) -> (returnType))? |
asType alias:
1 |
typealias closureType = (parameterTypes) -> (returnType) |
asconstant:
1 |
let closureName: closureType = { ... } |
asWhen the function call parameters:
1 |
func({(parameterTypes) -> (returnType) in statements}) |
asFunction parameters:
1 |
array.sort({ (item1: Int, item2: Int) -> Bool in return item1 < item2 }) |
asAnd using the parameters of the function type inference:
1 |
array.sort({ (item1, item2) -> Bool in return item1 < item2 }) |
asEstimation function parameters and return type:
1 |
array.sort({ (item1, item2) in return item1 < item2 }) |
asThe last parameter of the function:
1 |
array.sort { (item1, item2) in return item1 < item2 } |
As the last parameter to the functionParameter names and abbreviations:
1 |
array.sort { return $0 < $1 } |
And as a function of the last parameterAnd concluded that the return value:
1 |
array.sort { $0 < $1 } |
As the last parameter to the function,A presence function as a reference:
1 |
array.sort(<) |
As a function parameterWith default capture:
1 |
array.sort({ [unowned self] (item1: Int, item2: Int) -> Bool in return item1 < item2 }) |
As a function parameterWith default parameters to infer the type of capture and return type and:
1 |
array.sort({ [unowned self] in return item1 < item2 }) |
This site does not list ready to use all closures。
Original article written by LogStudio:R0uter's Blog » Interpret:How do I declare the Swift closure?
Reproduced Please keep the source and description link:https://www.logcg.com/archives/1055.html