-
Book Overview & Buying
-
Table Of Contents
Mastering Swift 6 - Seventh Edition
By :
Actors are a powerful concurrency feature that help manage state safely in concurrent environments. They are designed to handle mutable state while preventing common concurrency issues such as data races. By using actors, we ensure that only one thread can access the actor's mutable state at a time. This can greatly simplify the development of concurrent code.
Actors are reference types, similar to classes, so they can be used to share state. They can also have properties, methods, initializers, and subscripts, and can conform to protocols and be generic. However, unlike classes, actors do not support inheritance.
Actors ensure that mutable state within the actor is only accessed by a single thread at a time. A useful way to think about actors is that they pass messages to the instance rather than references to the instance.
Let’s look at an example of an Actor and the see the problem they are designed to solve. For this example we will create a very basic BankAccount...