You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift 6迁移时闭包作为‘sending’参数传递引发的数据竞争警告问题及修复咨询

Swift 6迁移时闭包作为‘sending’参数传递引发的数据竞争警告问题及修复咨询

嘿,我在迁移到Swift 6的过程中碰到了一个棘手的问题,目前还没找到解决思路。

收到的警告内容如下:

Passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution of the closure; this is an error in the Swift 6 language mode

我提炼出的示例代码是这样的:

@Observable
class Helloworld {
    var hello: Bool = false
    func world() {
        Task { // Compiler warning: "Passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution of the closure; this is an error in the Swift 6 language mode"
            await self.work()
        }
    }
    
    private func work() async {
        do {
            try await Task.sleep(for: .seconds(3))
            hello = true
        }
        catch {
            // Gotcha
        }
    }
}

有没有大佬能告诉我该怎么修复这个问题呀?

备注:内容来源于stack exchange,提问作者Jonny

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.15 10:13:10