Reputation:
Assuming a @nonescaping
variadic closure parameter as in
func method(_ closures: () -> Void...)
then mutating it to @escaping
as in
func method(_ closures: @escaping () -> Void...)
produces the following error.
@escaping attribute may only be used in function parameter position
Upvotes: 2
Views: 367
Reputation: 386038
You don't need to use @escaping
here at all. Only a closure that is directly an argument (with nothing wrapping it) can be non-escaping.
A closure that is part of a variadic argument is (under the hood) wrapped in an Array
, so it is already implicitly @escaping
.
For example, this compiles and runs just fine:
class MyObject {
var closures: [() -> ()] = []
func add(_ closures: () -> () ...) {
self.closures += closures
}
func run() {
for closure in closures { closure() }
}
}
let object = MyObject()
object.add({ print("first") }, { print("second") })
object.run()
Upvotes: 4