Complete the code to declare an actor in Swift.
actor [1] { var count = 0 }
The keyword actor declares an actor type named Counter.
Complete the code to safely increment the count inside the actor.
actor Counter {
var count = 0
func increment() {
[1]
}
}Inside an actor, use self.count += 1 to modify the property safely.
Fix the error in calling the actor's method asynchronously.
let counter = Counter()
Task {
await [1].increment()
}You must call the method on the actor instance counter using await because actor methods are asynchronous.
Fill both blanks to define an actor with a method that returns the count.
actor [1] { var count = 0 func getCount() async -> [2] { return count } }
The actor is named SafeCounter and the method returns an Int representing the count.
Fill all three blanks to create an actor that prevents data races by isolating state and providing safe access.
actor [1] { private var value: [2] = 0 func updateValue(to newValue: [3]) { value = newValue } }
The actor DataGuard isolates the Int state value and safely updates it through a method, preventing data races.