Store a closure as a variable in Swift Store a closure as a variable in Swift swift swift

Store a closure as a variable in Swift


The compiler complains on

var completionHandler: (Float)->Void = {}

because the right-hand side is not a closure of the appropriate signature, i.e. a closure takinga float argument. The following would assign a "do nothing" closure to thecompletion handler:

var completionHandler: (Float)->Void = {    (arg: Float) -> Void in}

and this can be shortened to

var completionHandler: (Float)->Void = { arg in }

due to the automatic type inference.

But what you probably want is that the completion handler is initialized to nilin the same way that an Objective-C instance variable is inititialized to nil. In Swiftthis can be realized with an optional:

var completionHandler: ((Float)->Void)?

Now the property is automatically initialized to nil ("no value").In Swift you would use optional binding to check of a thecompletion handler has a value

if let handler = completionHandler {    handler(result)}

or optional chaining:

completionHandler?(result)


Objective-C

@interface PopupView : UIView@property (nonatomic, copy) void (^onHideComplete)();@end@interface PopupView ()...- (IBAction)hideButtonDidTouch:(id sender) {    // Do something    ...    // Callback    if (onHideComplete) onHideComplete ();}@endPopupView * popupView = [[PopupView alloc] init]popupView.onHideComplete = ^() {    ...}

Swift

class PopupView: UIView {    var onHideComplete: (() -> Void)?    @IBAction func hideButtonDidTouch(sender: AnyObject) {        // Do something        ....        // Callback        if let callback = self.onHideComplete {            callback ()        }    }}var popupView = PopupView ()popupView.onHideComplete = {    () -> Void in     ...}


I've provide an example not sure if this is what you're after.

var completionHandler: (_ value: Float) -> ()func printFloat(value: Float) {    print(value)}completionHandler = printFloatcompletionHandler(5)

It simply prints 5 using the completionHandler variable declared.