I'm learning Swift lang, but I cannot pass optional callback argument into function:
func dismiss(completion: () -> Void) {
if (completion) {
return self.dismissViewControllerAnimated(true, completion: completion)
}
self.dismissModalViewControllerAnimated(true)
}
This shows me an error - Type () -> Void does not conform to protocol 'LogicValue'
Any suggestions?
Update for Swift 3/4:
An optional is no longer a boolean expression, and the deprecated
func dismissModalViewControllerAnimated(animated: Bool)
is no longer available in Swift.Simply declare the completion parameter as an optional closure, and pass it on to
which takes an optional closure as well:
Old (Swift 1.x?) answer:
Declare the
completion
parameter as (implicitly unwrapped) optional closure(() -> Void)!
:But note that you can call
in any case, because the
completion
parameter of that function is optional as well. Andis actually marked as deprecated.
It's better to add
= nil
in the callback declaration, to avoid passing nil while calling it:And you can call your function like this :
dismiss()
Just adding to Martin R's answer above..
The callback can be optional, instead of implicit parameter (with exclamation mark), use the optional operator.