Challenge - 5 Problems
Actor Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Understanding actor isolation in Swift
What will be the output of this Swift code using an actor to protect shared data?
Swift
actor Counter {
var value = 0
func increment() async {
value += 1
}
func getValue() async -> Int {
return value
}
}
let counter = Counter()
Task {
await counter.increment()
let val = await counter.getValue()
print(val)
}Attempts:
2 left
💡 Hint
Remember that actors serialize access to their mutable state.
✗ Incorrect
The actor ensures that only one task accesses or modifies its state at a time. The increment() method increases value by 1, and getValue() returns it. Both are awaited, so the printed value is 1.
🧠 Conceptual
intermediate1:30remaining
Why do actors prevent data races?
Which statement best explains why Swift actors prevent data races?
Attempts:
2 left
💡 Hint
Think about how actors control access to their internal data.
✗ Incorrect
Actors prevent data races by serializing access to their mutable state, so only one task can read or write at a time, avoiding simultaneous conflicting accesses.
🔧 Debug
advanced2:30remaining
Identify the data race in this Swift code
What error or problem will occur when running this code without using an actor?
Swift
class Counter { var value = 0 func increment() { value += 1 } } let counter = Counter() DispatchQueue.concurrentPerform(iterations: 1000) { _ in counter.increment() } print(counter.value)
Attempts:
2 left
💡 Hint
Consider what happens when multiple threads modify the same variable without synchronization.
✗ Incorrect
Without synchronization, multiple threads can modify value simultaneously, causing lost updates and a final value less than 1000.
📝 Syntax
advanced2:00remaining
Which code snippet correctly uses an actor to prevent data races?
Choose the code that correctly defines and uses an actor to safely increment a counter.
Attempts:
2 left
💡 Hint
Remember that actor methods that modify state are asynchronous and must be awaited.
✗ Incorrect
Option A correctly defines an actor with an async method and calls it with await. Option A misses async/await, C uses a class not an actor, and D calls async method without await.
🚀 Application
expert3:00remaining
Predict the final value with concurrent actor calls
Given this Swift code, what is the final printed value of the counter after all tasks complete?
Swift
actor Counter {
var value = 0
func increment() async {
let current = value
try? await Task.sleep(nanoseconds: 1_000_000) // simulate delay
value = current + 1
}
func getValue() async -> Int {
return value
}
}
let counter = Counter()
Task {
await withTaskGroup(of: Void.self) { group in
for _ in 1...10 {
group.addTask {
await counter.increment()
}
}
}
let finalValue = await counter.getValue()
print(finalValue)
}Attempts:
2 left
💡 Hint
Actors serialize access even if tasks run concurrently.
✗ Incorrect
Even though increments run concurrently, the actor serializes access to value, so all increments apply correctly, resulting in 10.