What does the nonisolated keyword mean in Swift concurrency?
It means the method can be called without switching to the actor's isolated context. The method runs without actor isolation, allowing safe access to non-isolated data.
Click to reveal answer
intermediate
Why use nonisolated methods inside an actor?
To allow some methods to run without waiting for actor isolation, improving performance for read-only or stateless operations that don't need exclusive access.
Click to reveal answer
intermediate
Can a nonisolated method access actor-isolated state directly?
No. Nonisolated methods cannot access actor-isolated properties or methods directly because they run outside the actor's isolation.
Click to reveal answer
beginner
Example: How to declare a nonisolated method in an actor?
Inside an actor, write nonisolated func methodName() { ... } to mark the method as nonisolated.
Click to reveal answer
beginner
What happens if you call a normal actor method from outside the actor?
The call is asynchronous and requires awaiting because the method runs in the actor's isolated context to ensure safe access.
Click to reveal answer
What is the main purpose of marking a method as nonisolated in a Swift actor?
ATo make the method asynchronous
BTo prevent the method from being called
CTo allow the method to run without actor isolation
DTo make the method private
✗ Incorrect
Nonisolated methods run outside the actor's isolation, allowing synchronous calls without awaiting.
Can a nonisolated method access actor-isolated properties directly?
AYes, always
BOnly if called from inside the actor
COnly if marked async
DNo, never
✗ Incorrect
Nonisolated methods cannot access actor-isolated state directly because they run outside the actor's isolation.
Which keyword do you use to mark a method as running outside actor isolation?
Anonisolated
Bisolated
Casync
Dprivate
✗ Incorrect
The nonisolated keyword marks methods that run outside actor isolation.
What happens when you call a normal actor method from outside the actor?
AIt runs synchronously
BIt requires awaiting because it runs asynchronously
CIt throws an error
DIt runs without isolation
✗ Incorrect
Normal actor methods require awaiting because they run asynchronously inside the actor's isolated context.
Which of these is a good use case for a nonisolated method?
APerforming stateless calculations
BReading actor-isolated properties
CModifying actor state
DCalling other isolated methods
✗ Incorrect
Nonisolated methods are good for stateless or read-only operations that don't need actor isolation.
Explain what a nonisolated method is in Swift actors and why you might use it.
Think about how actor isolation controls access and when you might want to bypass it safely.
You got /3 concepts.
Describe the difference between calling a normal actor method and a nonisolated method from outside the actor.
Consider how actor isolation affects method calls and concurrency.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of marking a method as nonisolated in a Swift actor?
easy
A. To allow the method to run outside the actor's isolation without needing await
B. To make the method private to the actor
C. To force the method to always run asynchronously
D. To prevent the method from accessing any properties
Solution
Step 1: Understand actor isolation
Actors protect their data by isolating it, so methods usually run inside this isolation.
Step 2: Role of nonisolated
Marking a method nonisolated means it runs outside this isolation and can be called without await.
Final Answer:
To allow the method to run outside the actor's isolation without needing await -> Option A
Quick Check:
Nonisolated method = runs outside isolation without await [OK]
Hint: Nonisolated means no await needed to call method [OK]
Common Mistakes:
Thinking nonisolated methods are private
Assuming nonisolated methods must be async
Believing nonisolated methods cannot access any properties
2. Which of the following is the correct way to declare a nonisolated method inside a Swift actor?
easy
A. func nonisolated greet() { print("Hello") }
B. nonisolated func greet() { print("Hello") }
C. func greet() nonisolated { print("Hello") }
D. func greet() async nonisolated { print("Hello") }
Solution
Step 1: Recall correct syntax for nonisolated
The nonisolated keyword comes before func in the method declaration.
Step 2: Check each option
nonisolated func greet() { print("Hello") } correctly places nonisolated before func. Others place it incorrectly or add unnecessary keywords.
Final Answer:
nonisolated func greet() { print("Hello") } -> Option B
Quick Check:
Keyword nonisolated before func = correct syntax [OK]
Hint: Put nonisolated before func keyword [OK]
Common Mistakes:
Placing nonisolated after func
Adding async unnecessarily
Using nonisolated as a function modifier
3. What will be the output of the following Swift code?
A. Compile error because printHello is nonisolated
B. Hello\n1
C. Hello
D. Runtime error because increment is not awaited
Solution
Step 1: Understand nonisolated method call
The method printHello() is marked nonisolated, so it can be called directly without await.
Step 2: Analyze output
Calling counter.printHello() prints "Hello". The increment method is commented out, so no other output.
Final Answer:
Hello -> Option C
Quick Check:
Nonisolated method prints Hello directly [OK]
Hint: Nonisolated methods run without await and print output [OK]
Common Mistakes:
Expecting increment output without await
Thinking nonisolated causes compile error
Confusing runtime error with compile error
4. Consider this Swift actor code snippet:
actor Logger {
var logs = [String]()
nonisolated func printLog(_ message: String) {
print(message)
}
func addLog(_ message: String) {
logs.append(message)
}
}
let logger = Logger()
logger.printLog("Start")
logger.addLog("Start")
What is the main issue with this code?
medium
A. addLog method must be marked nonisolated
B. Nonisolated method printLog cannot access actor's logs array
C. Nonisolated method printLog causes data race on logs
D. Cannot call addLog without await, causing a compile error
Solution
Step 1: Check method calls
The addLog method is isolated and must be called with await outside the actor.
Step 2: Identify error
Calling logger.addLog("Start") without await causes a compile error.
Final Answer:
Cannot call addLog without await, causing a compile error -> Option D
Quick Check:
Isolated method call requires await [OK]
Hint: Isolated methods need await; nonisolated do not [OK]
Common Mistakes:
Thinking nonisolated can access isolated properties
Assuming addLog can be called without await
Marking all methods nonisolated incorrectly
5. You want to add a nonisolated method to a Swift actor that returns a constant string without accessing any actor-isolated data. Which of the following implementations is correct and safe?
hard
A. nonisolated func status() -> String { return "Ready" }