0
0
Swiftprogramming~10 mins

Actor declaration syntax 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 simple actor named Logger.

Swift
actor [1] {
    func log(message: String) {
        print(message)
    }
}
Drag options to blanks, or click blank then click option'
Aenum Logger
Bclass Logger
Cstruct Logger
DLogger
Attempts:
3 left
💡 Hint
Common Mistakes
Using class, struct, or enum instead of actor.
2fill in blank
medium

Complete the code to declare an actor named DataStore with a property count initialized to 0.

Swift
actor DataStore {
    var count: Int = [1]
}
Drag options to blanks, or click blank then click option'
A0
Bnil
C"0"
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string "0" instead of the integer 0.
Using nil which is not valid for Int without optional.
3fill in blank
hard

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

Swift
actor [1] {
    func fetchData() async -> String {
        return "Data"
    }
}
Drag options to blanks, or click blank then click option'
Afunc DataFetcher
BDataFetcher
Cclass DataFetcher
Dstruct DataFetcher
Attempts:
3 left
💡 Hint
Common Mistakes
Adding keywords like func or class before the actor name.
4fill in blank
hard

Fill both blanks to declare an actor named Counter with a method increment that increases count by 1.

Swift
actor [1] {
    var count = 0
    func [2]() {
        count += 1
    }
}
Drag options to blanks, or click blank then click option'
ACounter
BincrementCount
Cincrement
Dincrease
Attempts:
3 left
💡 Hint
Common Mistakes
Using method names that don't match increment behavior.
Using wrong actor names.
5fill in blank
hard

Fill all three blanks to declare an actor named BankAccount with a balance property and a method deposit that adds amount to balance.

Swift
actor [1] {
    var [2]: Double = 0.0
    func [3](amount: Double) {
        balance += amount
    }
}
Drag options to blanks, or click blank then click option'
ABankAccount
Bbalance
Cdeposit
Dwithdraw
Attempts:
3 left
💡 Hint
Common Mistakes
Using withdraw instead of deposit for the method.
Using wrong property names.