0Pricing
Swift Academy · Lesson

Actors & data isolation, nonisolated

Learn actor isolation: state is protected behind an actor , cross-actor access requires await , and nonisolated members bypass isolation when they are pure or static.

Actors & data isolation, nonisolated is a free Swift Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What actors provide

An actor protects its mutable state. Only one task touches isolated state at a time; external callers must await its methods.

  • Isolation = no data races
  • Cross-actor calls are async
  • nonisolated for pure/static members

Actor basics

Inside the actor, access is direct. From outside, you must await both reads and writes of isolated state.

actor Counter {
    private var value: Int = 0   // isolated state

    func increment() { value += 1 }      // isolated method (async to outsiders)
    func get() -> Int { value }          // read isolated state
}

let c = Counter()
Task {
    await c.increment()
    print(await c.get())  // 1
}

Serialization guarantee

Even if multiple tasks call concurrently, the actor serializes access to its state, preventing races.

actor BankAccount {
    private var balance: Int = 0
    func deposit(_ amount: Int) { balance += amount }
    func withdraw(_ amount: Int) -> Bool {
        if balance >= amount { balance -= amount; return true }
        return false
    }
    func current() -> Int { balance }
}

let acc = BankAccount()
Task {
    async let a = acc.deposit(50)
    async let b = acc.withdraw(20)
    _ = await (a, b)                 // operations are serialized inside the actor
    print(await acc.current())       // deterministic balance
}

nonisolated APIs

Mark nonisolated for pure or static members so callers do not need await. They must not access isolated state.

actor Clock {
    private var ticks: Int = 0
    func tick() { ticks += 1 }
    func read() -> Int { ticks }

    // Nonisolated: safe without awaiting (pure computation / constant)
    nonisolated static let format = "HH:mm:ss"

    nonisolated func describeFormat() -> String {
        // Cannot touch isolated state here; this is pure
        return "Format: " + Self.format
    }
}

let cl = Clock()
Task {
    await cl.tick()
    print(await cl.read())           // 1
    // Nonisolated members don't require await:
    print(Clock.format)              // "HH:mm:ss"
    print(cl.describeFormat())       // sync call
}

Reentrancy awareness

Actors can be reentrant: after an await inside an actor method, other messages may run. Keep invariants valid across await points.

actor Logger {
    private var lines: [String] = []
    func log(_ s: String) async {
        lines.append(s)
        // Simulate an await point; actor may process other messages here (reentrant)
        try? await Task.sleep(nanoseconds: 10_000_000)
        lines.append("done")
    }
    func snapshot() -> [String] { lines }
}

let logger = Logger()
Task {
    async let a = logger.log("A")
    async let b = logger.log("B")
    _ = await (a, b)
    print(await logger.snapshot())   // order shows possible interleaving
}

Best practices

Guidelines:

  • Expose small async methods; avoid long critical sections.
  • Use nonisolated for constants/pure helpers.
  • Do not store non-Sendable shared mutable state outside actors (you will learn Sendable next).

Actor isolation requirement

Quick check: How do you read or write actor state from outside?

Recap

Recap: Actors isolate mutable state; external access requires await. Use nonisolated for pure/static members and design short async methods to keep reentrancy safe.

Frequently asked questions

Is the “Actors & data isolation, nonisolated” lesson free?

Yes — the full text of “Actors & data isolation, nonisolated” is free to read here on the web, and the Swift Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

What will I learn in “Actors & data isolation, nonisolated”?

Learn actor isolation: state is protected behind an actor , cross-actor access requires await , and nonisolated members bypass isolation when they are pure or static. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Actors & data isolation, nonisolated” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. TaskGroup for parallelism
  2. Actors & data isolation, nonisolated
  3. Sendable and thread-safety checking
← Back to Swift Academy