I would like to call an @autoclosure parameter inside dispatch_async
block.
func myFunc(@autoclosure condition: () -> Bool) {
dispatch_async(dispatch_get_main_queue()) {
if condition() {
println("Condition is true")
}
}
}
I get the following error.
Closure use of @noescape parameter may allow it to escape.
Is it possible to call @autoclosure parameter asynchronously?
Tested in Xcode 6.4 (6E23).
Yes, so long as you declare them @autoclosure(escaping)
:
Declarations with the
autoclosure
attribute implynoescape
as well, except when passed the optional attributeescaping
.
So this should do it:
func myFunc(@autoclosure(escaping) condition: () -> Bool) {
dispatch_async(dispatch_get_main_queue()) {
if condition() {
println("Condition is true")
}
}
}
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