Complete the code to declare an actor in Swift.
actor [1] { var count = 0 }
In Swift, actor is followed by the actor's name, like Counter.
Complete the code to call an async method on an actor instance.
let counter = Counter()
Task {
await counter.[1]()
}Actor methods that modify state are usually async, so calling increment() with await is correct.
Fix the error in the code to safely access shared data using a lock.
class SafeCounter { private var count = 0 private let lock = NSLock() func increment() { [1].lock() count += 1 lock.unlock() } }
The lock instance must be used to call lock() and unlock() to protect the critical section.
Fill in the blank to create a dictionary filter that selects keys with values greater than 10.
let filtered = dictionary.filter { $0.value [1] 10 }.map { ($0.key, $0.value) }
let result = Dictionary(uniqueKeysWithValues: filtered) // Result dictionaryTo filter values greater than 10, use the '>' operator in the filter closure.
Fill all three blanks to create an actor method that safely increments and returns the count.
actor Counter {
private var count = 0
func incrementAndGet() async -> Int {
[1]
count += 1
let current = [2]
[3]
return current
}
}Using await Task.sleep(nanoseconds: 0) and await Task.yield() simulates async suspension points. The count variable holds the current value after increment.