Declaration and Syntax
Actors are declared using theactor keyword. They share the same capabilities as classes—they can have properties, methods, initializers, subscripts, and conform to protocols—but they do not support inheritance.
Actor Isolation and Access Rules
The core mechanism of an actor is actor isolation. The Swift compiler enforces strict rules regarding how data inside an actor is accessed:- Internal Access (Synchronous): Code executing inside the actor (e.g., within its own methods) can read and mutate its properties synchronously.
- Cross-Actor Access (Asynchronous): Code executing outside the actor must interact with the actor asynchronously using the
awaitkeyword. This is because the calling task may need to be suspended until the actor’s executor is free to process the request.
The nonisolated Keyword
By default, all properties and methods of an actor are isolated to that actor. However, immutable state (declared with let) is implicitly safe to access concurrently. For methods or computed properties that do not access isolated mutable state, you can explicitly opt out of actor isolation using the nonisolated keyword.
A nonisolated member can be accessed synchronously from outside the actor.
Global Actors
Swift provides the@globalActor attribute to define singleton actors that can isolate state and operations across different types and functions scattered throughout a codebase. The most prominent built-in global actor is @MainActor, which binds execution to the main dispatch queue (the main thread).
Applying a global actor attribute to a declaration isolates it to that specific actor’s executor.
Actor Reentrancy
A critical architectural characteristic of Swift actors is that they are reentrant. If an actor executes an asynchronous call (await) within its own isolated context, the actor’s execution is suspended.
During this suspension point, the actor’s executor is yielded, allowing it to process other pending tasks in its mailbox. While this prevents deadlocks, it means the actor’s isolated state may mutate between the suspension point and the resumption of the original task. Developers must not assume that isolated state remains unchanged across an await boundary within an actor.
Tired of Poor Swift Skills? Fix That With Deep Grasping!Learn More





