@wwdc21 Protect mutable state with Swift actors
Asked on 2026-04-13
1 search
To protect mutable state with Swift actors, you can utilize the actor model introduced in Swift. Actors are reference types similar to classes, but they automatically protect their state by serializing accesses, ensuring that only a single task can execute at a time on an actor. This helps prevent data races, which can occur when multiple threads try to access and mutate shared data simultaneously.
In the session titled "A Swift Tour: Explore Swift’s features and design" from WWDC 2024, it is explained that actors encapsulate shared mutable state and provide a convenient way to ensure data race safety. Calls to actor methods from outside the actor's context are asynchronous, requiring the use of the await keyword. This ensures that concurrent accesses are synchronized, eliminating data race conditions (A Swift Tour: Explore Swift’s features and design).
Additionally, the session "What’s new in Swift" from WWDC 2025 discusses the use of the main actor to protect mutable state by default. This is particularly useful in projects that are mostly single-threaded, as it reduces the need for concurrency annotations and helps prevent data race safety errors (What’s new in Swift).
For more practical guidance, the session "Code-along: Elevate an app with Swift concurrency" provides examples of how to prevent data races by isolating mutable state to the main actor and using sendable value types (Code-along: Elevate an app with Swift concurrency).

What’s new in Swift
Join us for an update on Swift. We’ll talk about workflow improvements that make you more productive, and new and modernized library APIs for fundamental programming tasks. We’ll show examples of Swift adoption throughout more layers of the software stack. Finally, we’ll explore new language features for both improving approachability of concurrency, and achieving peak performance when you need it.

Code-along: Elevate an app with Swift concurrency
Learn how to optimize your app’s user experience with Swift concurrency as we update an existing sample app. We’ll start with a main-actor app, then gradually introduce asynchronous code as we need to. We’ll use tasks to optimize code running on the main actor, and discover how to parallelize code by offloading work to the background. We’ll explore what data-race safety provides, and work through interpreting and fixing data-race safety errors. Finally, we’ll show how you can make the most out of structured concurrency in the context of an app.

A Swift Tour: Explore Swift’s features and design
Learn the essential features and design philosophy of the Swift programming language. We’ll explore how to model data, handle errors, use protocols, write concurrent code, and more while building up a Swift package that has a library, an HTTP server, and a command line client. Whether you’re just beginning your Swift journey or have been with us from the start, this talk will help you get the most out of the language.
