Actor initializer

actor Person {
        
    init() {
        testMethod() // warning! Actor-isolated instance method 'testMethod()' can not be referenced from a non-isolated context; this is an error in Swift 6
    }
    
    func testMethod() {}
    
}

Actor properties and instance methods are known to be isolated.
Is Actor's initializer non-isolated??

  • using Xcode 14.2

Synchronous actor initializers cannot hop on the actor's executor, so it runs in a non-isolated context.

An asynchronous initializer can use the executor after all properties have been initialized. Make your init async and it should work:

actor Person {
        
    init() async {
        testMethod()
    }
    
    func testMethod() {}
    
}
1 Like