We can declare block as below in Objective-C.
typedef void (^CompletionBlock) (NSString* completionReason);
I\'m trying to do this in swift
Here is awesome blog about swift closure.
Here are some examples:
As a variable:
var closureName: (inputTypes) -> (outputType)
As an optional variable:
var closureName: ((inputTypes) -> (outputType))?
As a type alias:
typealias closureType = (inputTypes) -> (outputType)
The syntax for function types is (in) -> out
.
typealias CompletionBlock = (NSString?) -> Void
// or
typealias CompletionBlock = (result: NSData?, error: NSError?) -> Void
var completion: CompletionBlock = { reason in print(reason) }
var completion: CompletionBlock = { result, error in print(error) }
Note that the parentheses around the input type are only required as of Swift 3+.