In addition to function dispatch_async
, that submits a block for asynchronous execution, iOS provides another function dispatch_async_f
to submit a function with a single parameter for asynchronous execution.
in Swift, I can call dispatch_async
as DispatchQueue.global().async {}
, but I did not find any way to call dispatch_async_f
.
So, how do I pass a parameter to a block executed asynchronously?
Asked
Active
Viewed 1,189 times
2

Reinhard Männer
- 14,022
- 5
- 54
- 116
-
You don't need that in Swift. Just `DispatchQueue.global().async { yourFunction(yourParameter) }` – Martin R Jan 06 '20 at 10:36
-
Ahh! So simple! Thanks for your quick answer! – Reinhard Männer Jan 06 '20 at 10:39
1 Answers
1
dispatch_async_f()
can be used in C code, which has no blocks or closures.
In Swift you simply pass a closure, and the closure calls the function:
DispatchQueue.global().async {
let theParameter = ...
theFunction(theParameter)
}
The closure can also capture values when created:
let theParameter = ...
DispatchQueue.global().async {
theFunction(theParameter)
}

Martin R
- 529,903
- 94
- 1,240
- 1,382