Challenge - 5 Problems
Swift Actor Isolation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a nonisolated method in an actor
What is the output of this Swift code using a nonisolated method inside an actor?
Swift
actor Counter {
var value = 0
nonisolated func description() -> String {
"Counter value is not accessible here"
}
func increment() {
value += 1
}
}
let counter = Counter()
print(counter.description())Attempts:
2 left
💡 Hint
Remember that nonisolated methods run outside the actor's isolation context.
✗ Incorrect
The nonisolated method description() is allowed to run without actor isolation, so it returns the fixed string. It cannot access the actor's isolated state 'value'.
❓ Predict Output
intermediate2:00remaining
Accessing actor state from nonisolated method
What happens if a nonisolated method tries to access an actor's isolated property directly?
Swift
actor Logger {
var logs = [String]()
nonisolated func latestLog() -> String {
logs.last ?? "No logs"
}
}
let logger = Logger()
print(logger.latestLog())Attempts:
2 left
💡 Hint
Nonisolated methods cannot access actor-isolated properties directly.
✗ Incorrect
The nonisolated method tries to access 'logs', which is actor-isolated. This causes a compile-time error because actor isolation rules prevent this access.
🔧 Debug
advanced2:00remaining
Why does this nonisolated method cause a compile error?
Identify the reason for the compile error in this code snippet.
Swift
actor BankAccount {
var balance: Int = 1000
nonisolated func getBalance() -> Int {
balance
}
}Attempts:
2 left
💡 Hint
Think about actor isolation rules and what nonisolated means.
✗ Incorrect
Nonisolated methods run outside the actor's isolation, so they cannot access isolated properties directly. This causes a compile error.
❓ Predict Output
advanced2:00remaining
Effect of nonisolated on method concurrency
What is the output of this Swift code involving a nonisolated method and an actor?
Swift
actor Printer {
nonisolated func printMessage() {
print("Hello from nonisolated method")
}
func printIsolatedMessage() {
print("Hello from isolated method")
}
}
let printer = Printer()
printer.printMessage()
Task {
await printer.printIsolatedMessage()
}Attempts:
2 left
💡 Hint
Nonisolated methods can be called directly without await.
✗ Incorrect
The nonisolated method runs immediately and prints its message. The isolated method requires await and runs asynchronously, printing its message after.
🧠 Conceptual
expert2:00remaining
Why use nonisolated methods in actors?
Which of the following best explains the purpose of marking a method as nonisolated inside a Swift actor?
Attempts:
2 left
💡 Hint
Think about when you want to avoid the overhead of actor isolation.
✗ Incorrect
Nonisolated methods run outside the actor's isolation, so they can be called synchronously without awaiting. They are useful for operations that do not access or modify isolated state.