Cannot form key path to actor-isolated property 'state'

Asked on 05/22/2025

1 search

The error "Cannot form key path to actor-isolated property 'state'" is related to Swift's concurrency model, particularly when dealing with actor isolation. In Swift, actors are used to protect mutable state and ensure that it is accessed in a thread-safe manner. When a property is actor-isolated, it means that it can only be accessed within the context of that actor.

From the session "Migrate your app to Swift 6" at WWDC 2024, there are several strategies discussed to handle such concurrency issues:

  1. Main Actor Isolation: If a function or property is isolated to the main actor, you need to ensure that any access to it is done on the main actor. This can be achieved by marking functions as async and using await to access actor-isolated properties. Alternatively, you can annotate the function to run on the main actor directly. See more in the session (16:50).

  2. Non-Isolated Methods: If you have a method on an actor-isolated type that does not need to be isolated, you can declare it as non-isolated. This is useful for callbacks that do not make promises about where they are called from. Learn more about this approach (17:12).

  3. Using assumeIsolated: When you know for certain that a call will be on a particular actor, you can use assumeIsolated to inform the compiler that the code is already running on the correct actor, avoiding the need to start a new task. Explore this method (23:23).

These strategies help manage actor isolation and ensure that your code adheres to Swift's concurrency model, preventing data races and ensuring thread safety.