Complete the code to declare an actor named Counter.
actor [1] { var value = 0 }
The keyword actor declares an actor type named Counter.
Complete the code to define an isolated method increment() inside the actor.
actor Counter {
var value = 0
func [1]() {
value += 1
}
}The method increment() increases the value by 1 inside the actor.
Fix the error by completing the code to call the isolated method increment() from outside the actor.
let counter = Counter()
Task {
await counter.[1]()
}Calling an isolated actor method from outside requires await and the method name increment().
Fill both blanks to define an isolated computed property and access it asynchronously.
actor Counter {
var value = 0
var [1]: Int {
return value
}
}
let counter = Counter()
Task {
let currentValue = await counter.[2]
}The isolated computed property is named value and accessed asynchronously with await counter.value.
Fill all three blanks to define an actor with an isolated method, call it asynchronously, and print the result.
actor Counter {
var value = 0
func [1]() {
value += 1
}
}
let counter = Counter()
Task {
await counter.[2]()
print(counter.[3])
}The method increment() increases the value, which is then printed by accessing value.