Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional closures in Swift 4 [duplicate]

Tags:

ios

swift

swift4

I need a closure method executed on demand.

In Swift 3 I used to do something like this:

// Declare closure
var checksPerformed: ((Void) -> Void)? // Declaration

// Call when needed
checksPerformed?() 

//Only executes when checksPerformed is called
checksPerformed = { _ in 
    // do stuff here
}

In Swift 4 this is no longer the case.

enter image description here

And after fixing this warning nothing works as before. What is the new way of doing this?

If I'm updating the declaration to: var checksPerformed: (() -> ())? I'm getting

enter image description here

like image 298
kernelpanic Avatar asked Oct 24 '25 18:10

kernelpanic


1 Answers

Distinguish between single-tuple and multiple-argument function types

// Declare closure
var checksPerformed: (() -> ())? // Declaration

// Call when needed
checksPerformed?()

//Only executes when checksPerformed is called
checksPerformed = {
    // do stuff here
}
like image 164
Nazmul Hasan Avatar answered Oct 26 '25 07:10

Nazmul Hasan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!