代码之家  ›  专栏  ›  技术社区  ›  Reinhard Männer

如何将参数传递给在Swift中异步执行的块?

  •  0
  • Reinhard Männer  · 技术社区  · 5 年前

    除了 function dispatch_async function dispatch_async_f 为异步执行提交带有单个参数的函数。
    在斯威夫特,我可以打电话 dispatch_async 作为 DispatchQueue.global().async {} ,但我没找到打电话的方法 dispatch_async_f .
    那么,如何将参数传递给异步执行的块?

    1 回复  |  直到 5 年前
        1
  •  0
  •   Martin R    5 年前

    dispatch_async_f() 可用于没有块或闭包的C代码中。

    在Swift中,您只需传递一个闭包,闭包就会调用函数:

    DispatchQueue.global().async {
        let theParameter = ...
        theFunction(theParameter)
    }
    

    let theParameter = ...
    DispatchQueue.global().async {
        theFunction(theParameter)
    }