0
0
Swiftprogramming~10 mins

Distributed actors overview in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a distributed actor named Logger.

Swift
distributed [1] Logger {
    distributed func log(message: String) async
}
Drag options to blanks, or click blank then click option'
Aclass
Bactor
Cstruct
Denum
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'class' or 'struct' instead of 'actor' to declare a distributed actor.
2fill in blank
medium

Complete the code to call a distributed actor's method asynchronously.

Swift
let logger = Logger()
await logger.[1](message: "Hello")
Drag options to blanks, or click blank then click option'
Asend
Bprint
Clog
Dwrite
Attempts:
3 left
💡 Hint
Common Mistakes
Using method names that are not defined in the actor like 'send' or 'print'.
3fill in blank
hard

Fix the error in the distributed actor declaration by completing the code.

Swift
distributed actor Logger {
    distributed func log(message: String) [1]
}
Drag options to blanks, or click blank then click option'
Aasync
Bthrows
Cstatic
Dmutating
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to mark distributed actor methods as async.
4fill in blank
hard

Fill both blanks to create a distributed actor with an initializer and a method.

Swift
distributed actor Logger {
    let id: String
    init([1]: String) {
        self.id = [2]
    }
    distributed func log(message: String) async {}
}
Drag options to blanks, or click blank then click option'
Aid
Bmessage
Dself.id
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'message' as the initializer parameter instead of 'id'.
Assigning 'self.id' to 'self.id' instead of the parameter.
5fill in blank
hard

Fill all three blanks to create a distributed actor with a method that returns a greeting.

Swift
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])
Drag options to blanks, or click blank then click option'
Aname
Bgreet
Cgreeting
Dlog
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names in the string or print statement.
Calling a method name that does not exist.