Swift, actor: Actor-isolated 属性 'scanning' cannot be mutated from a non-isolated context
Swift, actor: Actor-isolated property 'scanning' can not be mutated from a non-isolated context
我有一个演员:
actor StatesActor {
var job1sActive:Bool = false
...
}
我有一个使用该演员的对象:
class MyObj {
let myStates = StatesActor()
func job1() async {
myStates.job1IsActive = true
}
}
行:
myStates.job1IsActive = true
出现此错误:
Actor-isolated property 'job1IsActive' can not be mutated from a non-isolated context
如何使用 actor store/read 正确地声明信息,以便 MyObj 可以使用它来读取和设置状态?
How can I use an actor to store/read state information correctly so the MyObj can use it to read and set state?
您不能从 actor 外部改变 actor 的实例变量。这就是演员的全部意义!
相反,给角色一个方法来设置它的own实例变量。然后您将能够调用该方法(使用 await
)。
我有一个演员:
actor StatesActor {
var job1sActive:Bool = false
...
}
我有一个使用该演员的对象:
class MyObj {
let myStates = StatesActor()
func job1() async {
myStates.job1IsActive = true
}
}
行:
myStates.job1IsActive = true
出现此错误:
Actor-isolated property 'job1IsActive' can not be mutated from a non-isolated context
如何使用 actor store/read 正确地声明信息,以便 MyObj 可以使用它来读取和设置状态?
How can I use an actor to store/read state information correctly so the MyObj can use it to read and set state?
您不能从 actor 外部改变 actor 的实例变量。这就是演员的全部意义!
相反,给角色一个方法来设置它的own实例变量。然后您将能够调用该方法(使用 await
)。