how mutable property can be sendable?

Asked on 10/17/2024

1 search

In Swift, a mutable property can be made sendable by ensuring that it protects its state from concurrent accesses. This is important to avoid data races, which can lead to crashes or unpredictable behavior. In Swift 6, data race safety is verified at compile time, and one way to achieve this is by marking types as sendable.

A type can qualify as sendable if it uses mechanisms like locks to manage access to its mutable state. Alternatively, Swift provides a feature called actors, which are reference types that encapsulate shared mutable state and ensure safe concurrent access. By using actors, you can make a mutable property sendable because actors handle synchronization for you.

For public types, Swift requires you to explicitly add sendable conformances because marking a type as sendable is a guarantee to your clients that the type contains no mutable state that could lead to data races. This explicit conformance is necessary to avoid prematurely locking in sendability, especially if you might want to change the type's mutability in the future.

For more details, you can refer to the session Migrate your app to Swift 6 (32:27).