0
0
Swiftprogramming~20 mins

Global actors (@MainActor) in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Main Actor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
ACompilation error due to missing await
BValue is now 0
CValue is now 1
DRuntime error due to data race
Attempts:
2 left
💡 Hint
Remember that @MainActor ensures code runs on the main thread and that await is needed when calling async methods.
🧠 Conceptual
intermediate
1:30remaining
Which statement about @MainActor is true?
Choose the correct statement about the @MainActor attribute in Swift concurrency.
A@MainActor requires all methods to be synchronous.
B@MainActor makes code run on a background thread automatically.
C@MainActor disables concurrency for the annotated code.
D@MainActor ensures the annotated code runs on the main thread.
Attempts:
2 left
💡 Hint
Think about what the 'Main' in @MainActor refers to.
🔧 Debug
advanced
2: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()
ACall to actor-isolated property 'data' from outside the actor requires 'await'
BSyntax error: missing async keyword on updateData()
CNo error, code runs fine
DRuntime error: data race detected
Attempts:
2 left
💡 Hint
Accessing actor-isolated properties from outside requires special handling.
📝 Syntax
advanced
1: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?
Afunc @MainActor updateUI() { print("UI updated") }
B@MainActor func updateUI() { print("UI updated") }
Cfunc updateUI() @MainActor { print("UI updated") }
Dfunc updateUI() { @MainActor print("UI updated") }
Attempts:
2 left
💡 Hint
Remember how attributes are placed before declarations in Swift.
🚀 Application
expert
2: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)
A3
B2
C1
D0
Attempts:
2 left
💡 Hint
Consider how many times greet() is called and that Task.detached can call @MainActor functions with await.