I have a function which takes two parameters, the last parameter is a callback closure:
func myAsycTask(name: String, callback: @escaping ()->Void) {
myQueue.async{
self.doTask(name)
callback()
}
}
func doTask(name: String) {...}
I would like to make the 2nd callback closure parameter optional. I tried to re-define the function above to:
func myAsycTask(name: String, callback: @escaping ()->Void? = nil) {
myQueue.async{
self.doTask(name)
callback()
}
}
I get a compiler error:
Nil default argument value of cannot be converted to type '
() ->
'
How can I achieve what I need then?
To declare optional function parameters in JavaScript, there are two approaches: Using the Logical OR operator ('||'): In this approach, the optional parameter is Logically ORed with the default value within the body of the function. Note: The optional parameters should always come at the end on the parameter list.
Because closures can be used just like strings and integers, you can pass them into functions. The syntax for this can hurt your brain at first, so we're going to take it slow. If we wanted to pass that closure into a function so it can be run inside that function, we would specify the parameter type as () -> Void .
Your current code means that Void
is an optional return in the closure (which does not make much sense, since Void
is already nothing). You should enclose the parameter in brackets and then make it optional.
func myAsycTask(name: String, callback: (() -> Void)? = nil)
Try making your callback closure Optional and remove @escaping. @escaping annotation is pointless because your parameter is basically an enum (Optional is an enum with 2 cases: some(Value) and none) If your closure is owned by another type it is implicitly escaping.
import UIKit
// Also you can use a typealias to keep code more readable
typealias Callback = (() -> Void)
class Test {
let myQueue = DispatchQueue(label: "com.playground")
func doTask(name: String) {
// something...
}
func myAsycTask(name: String, callback: Callback? = nil) {
myQueue.async { [weak self] in
self?.doTask(name: name)
callback?()
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With