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