Complete the code to declare a distributed actor named Logger.
distributed [1] Logger {
distributed func log(message: String) async
}In Swift, distributed actors are declared using the distributed actor keywords. Here, actor is the correct keyword to define a distributed actor.
Complete the code to call a distributed actor's method asynchronously.
let logger = Logger() await logger.[1](message: "Hello")
The method defined in the distributed actor is log(message:), so calling log is correct.
Fix the error in the distributed actor declaration by completing the code.
distributed actor Logger {
distributed func log(message: String) [1]
}Distributed actor methods must be asynchronous, so the async keyword is required.
Fill both blanks to create a distributed actor with an initializer and a method.
distributed actor Logger {
let id: String
init([1]: String) {
self.id = [2]
}
distributed func log(message: String) async {}
}The initializer parameter name should be 'id' and the assignment should use the parameter 'id' to set the property.
Fill all three blanks to create a distributed actor with a method that returns a greeting.
distributed actor Greeter {
let name: String
init(name: String) {
self.name = name
}
distributed func greet() async -> String {
return "Hello, [1]!"
}
}
let greeter = Greeter(name: "Swift")
let greeting = await greeter.[2]()
print([3])The method returns a greeting using the 'name' property. The method called is 'greet', and the printed variable is 'greeting'.