Swift软件包管理器:如何使用异步代码运行ExecutableTarget?

tf7tbtn2  于 2023-03-17  发布在  Swift
关注(0)|答案(1)|浏览(133)

我正在向我的包中添加一个executableTarget,并希望在其中运行一些异步代码,如下所示:

Task {
    await Test.launch()
}

struct Test {
    static func launch() async {
        let result = await result()
        // ...
    }
}

但是这段代码实际上从来没有被调用过,程序在此之前就结束了。
我该怎么办?
谢谢你的帮忙

osh3o9ms

osh3o9ms1#

假设这是顶级代码,您应该删除围绕异步调用的Task

await Test.launch()

struct Test {
    static func launch() async {
        let result = await result()
        // ...
    }
}

自从SE-0343 Concurrency in Top-level Code在Swift 5.7中实现以来,这就得到了支持。

相关问题