我是 Swift 并发的新手(就像大多数人一样,我想),我遇到了一个编译器错误,我不知道该怎么办。
struct Thing {
var counter = 0
mutating func increment() async {
counter += 1
}
}
class Controller: UIViewController {
var thing = Thing()
func mutate() async {
await thing.increment()
print(thing.counter)
}
}
let c = Controller()
Task {
await c.mutate()
}
该mutate()
函数的第一行给了我以下错误。
Actor-isolated property 'thing' cannot be passed 'inout' to 'async' function call
如果我只是继承class
而不是UIViewController
正常工作,但我需要这里的控制器,所以我需要弄清楚如何在特定的上下文中使其工作。