Swift 6 · Errors and concurrency

Protect shared state with an actor

An actor processes one call at a time, which prevents data races. Call its methods with await.

actor Counter {
    private var value = 0
    func increment() -> Int {
        value += 1
        return value
    }
}

let count = await counter.increment()

Official Swift 6 documentation

More errors and concurrency commands

All errors and concurrency commands · Swift 6 cheatsheet

Was this page helpful?