如何将参数传递给在 Swift 中异步执行的块?
How to pass a parameter to a block executed asynchronously in Swift?
除了function dispatch_async
, that submits a block for asynchronous execution, iOS provides another function dispatch_async_f
提交一个异步执行的单参数函数。
在 Swift 中,我可以将 dispatch_async
称为 DispatchQueue.global().async {}
,但我没有找到任何方法来调用 dispatch_async_f
。
那么,如何将参数传递给异步执行的块?
dispatch_async_f()
可以在没有块或闭包的 C 代码中使用。
在Swift中你简单地传递一个闭包,闭包调用函数:
DispatchQueue.global().async {
let theParameter = ...
theFunction(theParameter)
}
闭包也可以在创建时捕获值:
let theParameter = ...
DispatchQueue.global().async {
theFunction(theParameter)
}
除了function dispatch_async
, that submits a block for asynchronous execution, iOS provides another function dispatch_async_f
提交一个异步执行的单参数函数。
在 Swift 中,我可以将 dispatch_async
称为 DispatchQueue.global().async {}
,但我没有找到任何方法来调用 dispatch_async_f
。
那么,如何将参数传递给异步执行的块?
dispatch_async_f()
可以在没有块或闭包的 C 代码中使用。
在Swift中你简单地传递一个闭包,闭包调用函数:
DispatchQueue.global().async {
let theParameter = ...
theFunction(theParameter)
}
闭包也可以在创建时捕获值:
let theParameter = ...
DispatchQueue.global().async {
theFunction(theParameter)
}