Nonisolated methods let you run code without waiting for other tasks. They help when you want to do simple things fast without blocking others.
Nonisolated methods in Swift
nonisolated func methodName() { // code here }
The nonisolated keyword tells Swift this method can run without isolation.
Use it only when the method does not access or change shared data.
actor MyActor {
nonisolated func greet() {
print("Hello from nonisolated method!")
}
}description method does not access count, so it can be nonisolated.actor Counter {
var count = 0
nonisolated func description() -> String {
return "Counter actor"
}
}This program shows a Logger actor with a nonisolated method logStart and an isolated method logMessage. You can call logStart directly without await, but you must use await for logMessage.
actor Logger {
nonisolated func logStart() {
print("Starting...")
}
func logMessage(_ message: String) {
print("Log: \(message)")
}
}
let logger = Logger()
// Call nonisolated method without await
logger.logStart()
// Call isolated method with await
Task {
await logger.logMessage("Hello")
}Nonisolated methods cannot access or change actor's isolated data safely.
Use nonisolated only for read-only or independent code.
Calling isolated methods requires await, but nonisolated methods do not.
Nonisolated methods run outside actor isolation.
They are useful for simple, safe code that doesn't touch shared data.
Nonisolated methods can be called without await.
Practice
nonisolated in a Swift actor?Solution
Step 1: Understand actor isolation
Actors protect their data by isolating it, so methods usually run inside this isolation.Step 2: Role of
Marking a methodnonisolatednonisolatedmeans it runs outside this isolation and can be called withoutawait.Final Answer:
To allow the method to run outside the actor's isolation without needingawait-> Option AQuick Check:
Nonisolated method = runs outside isolation without await [OK]
- Thinking nonisolated methods are private
- Assuming nonisolated methods must be async
- Believing nonisolated methods cannot access any properties
nonisolated method inside a Swift actor?Solution
Step 1: Recall correct syntax for nonisolated
Thenonisolatedkeyword comes beforefuncin the method declaration.Step 2: Check each option
nonisolated func greet() { print("Hello") } correctly placesnonisolatedbeforefunc. Others place it incorrectly or add unnecessary keywords.Final Answer:
nonisolated func greet() { print("Hello") } -> Option BQuick Check:
Keyword nonisolated before func = correct syntax [OK]
- Placing nonisolated after func
- Adding async unnecessarily
- Using nonisolated as a function modifier
actor Counter {
var count = 0
nonisolated func printHello() {
print("Hello")
}
func increment() {
count += 1
}
}
let counter = Counter()
counter.printHello()
// counter.increment() // requires await
Solution
Step 1: Understand nonisolated method call
The methodprintHello()is markednonisolated, so it can be called directly withoutawait.Step 2: Analyze output
Callingcounter.printHello()prints "Hello". The increment method is commented out, so no other output.Final Answer:
Hello -> Option CQuick Check:
Nonisolated method prints Hello directly [OK]
- Expecting increment output without await
- Thinking nonisolated causes compile error
- Confusing runtime error with compile error
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?
Solution
Step 1: Check method calls
TheaddLogmethod is isolated and must be called withawaitoutside the actor.Step 2: Identify error
Callinglogger.addLog("Start")withoutawaitcauses a compile error.Final Answer:
Cannot call addLog without await, causing a compile error -> Option DQuick Check:
Isolated method call requires await [OK]
- Thinking nonisolated can access isolated properties
- Assuming addLog can be called without await
- Marking all methods nonisolated incorrectly
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?Solution
Step 1: Identify safe nonisolated method
A nonisolated method must not access actor-isolated data likelogs.Step 2: Evaluate options
nonisolated func status() -> String { return "Ready" } returns a constant string without accessing isolated data, so it's safe and correct.Final Answer:
nonisolated func status() -> String { return "Ready" } -> Option AQuick Check:
Nonisolated method returns constant, no isolated data access [OK]
- Accessing isolated properties inside nonisolated methods
- Making nonisolated methods async unnecessarily
- Ignoring actor isolation rules
Ready to prove it?
Pass the test and this pattern moves unknown
