Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to mark the function as running on the main actor.
Swift
@[1] func updateUI() { print("UI updated") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @GlobalActor instead of @MainActor
Forgetting the '@' symbol before MainActor
Using unrelated actor names
✗ Incorrect
The @MainActor attribute ensures the function runs on the main thread, which is important for UI updates.
2fill in blank
mediumComplete the code to declare a global actor named MyActor.
Swift
@globalActor
struct [1] {
static let shared = MyActor()
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different name than the shared instance
Confusing with built-in actors like MainActor
✗ Incorrect
The struct name must match the global actor name, here 'MyActor'.
3fill in blank
hardFix the error in the code to ensure the function runs on the main actor.
Swift
[1] func updateData() async { print("Data updated") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @globalActor which is not a valid attribute
Placing async incorrectly
Using a non-existent actor like @UIActor
✗ Incorrect
Adding @MainActor before the function ensures it runs on the main actor.
4fill in blank
hardFill both blanks to declare a global actor and use it on a function.
Swift
@globalActor struct [1] { static let shared = [1]() } @[2] func performTask() { print("Task performed") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for struct and shared instance
Using @MainActor instead of the custom actor
✗ Incorrect
The struct name and shared instance must match, and the function uses the declared global actor attribute.
5fill in blank
hardFill all three blanks to create a global actor, mark a class with it, and declare an async function.
Swift
@globalActor struct [1] { static let shared = [1]() } [2] class DataManager { func fetchData() [3] { print("Fetching data") } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting '@' before the actor name on the class
Mismatching struct and shared instance names
Omitting async keyword on the function
✗ Incorrect
The struct and shared instance names must match, the class is marked with the global actor attribute, and the function is async.