In new Xcode UIViewController and other UIView classes are marked as @MainActor by default:
@MainActor class UIViewController : UIResponder
But it seems no to be the same as marking UIViewController as @MainActror explicitly in code.
This code compiles:
class VC: UIViewController {
var array: [Int] = []
}
class Test {
let vc = VC()
func doSomething() {
vc.array.append(1)
}
}
but after adding "@MainActor" to VC:
@MainActor class VC: UIViewController {
var array: [Int] = []
}
class Test {
let vc = VC()
func doSomething() {
vc.array.append(1)
}
}
I got an error:
Property 'array' isolated to global actor 'MainActor' can not be mutated from this context
There’s a delicate balance here between enforcing the Swift concurrency rules and ‘breaking the world’. Adding an explicit @MainActor
tells Swift that you’re concurrency aware and thus it should work harder to enforce the rules.
However, I must stress that not all of the Swift concurrency road has been laid yet, so opting in to concurrency checking right now won’t be a smooth path. This situation is actively evolving over on Swift Forums as I type — see Concurrency in Swift 5 and 6 for the background and this thread for the very latest — and I encourage you to get involved.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"