Challenge - 5 Problems
Main Actor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Swift code using @MainActor?
Consider this Swift code snippet using the @MainActor attribute. What will be printed when the program runs?
Swift
import Foundation @MainActor class Counter { var value = 0 func increment() { value += 1 print("Value is now \(value)") } } let counter = Counter() Task { await counter.increment() } // Wait briefly to allow Task to run Thread.sleep(forTimeInterval: 0.1)
Attempts:
2 left
💡 Hint
Remember that @MainActor ensures code runs on the main thread and that await is needed when calling async methods.
✗ Incorrect
The @MainActor attribute means the increment() method runs on the main thread. The call to increment() is awaited inside a Task, so it runs correctly and prints "Value is now 1".
🧠 Conceptual
intermediate1:30remaining
Which statement about @MainActor is true?
Choose the correct statement about the @MainActor attribute in Swift concurrency.
Attempts:
2 left
💡 Hint
Think about what the 'Main' in @MainActor refers to.
✗ Incorrect
The @MainActor attribute guarantees that the annotated code runs on the main thread, which is important for UI updates and thread safety.
🔧 Debug
advanced2:00remaining
What error does this code produce?
This Swift code uses @MainActor but has a problem. What error will the compiler show?
Swift
import Foundation @MainActor class DataManager { var data = "" func updateData() { data = "Updated" } } let manager = DataManager() manager.updateData()
Attempts:
2 left
💡 Hint
Accessing actor-isolated properties from outside requires special handling.
✗ Incorrect
Because DataManager is isolated to @MainActor, accessing its properties or methods from outside requires 'await' and async context. Calling updateData() directly causes a compiler error.
📝 Syntax
advanced1:30remaining
Which option correctly declares a global function on the main actor?
You want to declare a global function that always runs on the main actor. Which syntax is correct?
Attempts:
2 left
💡 Hint
Remember how attributes are placed before declarations in Swift.
✗ Incorrect
The correct syntax places @MainActor before the function declaration. Other options are invalid syntax.
🚀 Application
expert2:30remaining
How many times is 'print' called in this code?
Analyze this Swift code using @MainActor and async calls. How many times will "Hello from main actor" be printed?
Swift
import Foundation @MainActor func greet() { print("Hello from main actor") } func run() async { await greet() Task.detached { await greet() } await greet() } Task { await run() } // Wait to allow async tasks to complete Thread.sleep(forTimeInterval: 0.2)
Attempts:
2 left
💡 Hint
Consider how many times greet() is called and that Task.detached can call @MainActor functions with await.
✗ Incorrect
The greet() function is called three times: twice directly awaited in run(), and once inside Task.detached awaited. All calls print the message.