在swift中如何声明闭包(closure)

个人理解swift中的闭包就是oc中的block

[1]声明一个闭包:

var closureName: (ParameterTypes) -> (ReturnType)
例如:

private var doSomethingHandler: (NSError?) -> (Void)

[2]声明一个可为空的闭包:

var closureName: ((ParameterTypes) -> (ReturnType))?
例如:

  private var doSomethingHandler: ((NSError?) -> (Void))?

[3]给闭包取别名:

typealias ClosureType = (ParameterTypes) -> (ReturnType)
例如:

  typealias doSomethingHandler = (NSError?) -> (Void)

form: http://fuckingclosuresyntax.com/

你可能感兴趣的:(在swift中如何声明闭包(closure))