0
0
Swiftprogramming~20 mins

MainActor for UI work in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MainActor Mastery
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 @MainActor to update UI state. What will be printed?
Swift
import Foundation

@MainActor
class ViewModel {
    var count = 0

    func increment() {
        count += 1
        print("Count is now \(count)")
    }
}

let vm = ViewModel()
Task {
    await vm.increment()
}
ACount is now 1
BCount is now 0
CCompilation error due to missing @MainActor on Task
DRuntime error due to accessing count off main thread
Attempts:
2 left
💡 Hint
Remember @MainActor ensures code runs on the main thread for UI safety.
🧠 Conceptual
intermediate
1:30remaining
Why use @MainActor for UI updates in Swift?
Which of these best explains why @MainActor is used for UI work in Swift concurrency?
AIt automatically updates UI elements without explicit calls.
BIt makes UI code run faster by using multiple threads.
CIt disables concurrency for UI code to simplify logic.
DIt guarantees UI code runs on the main thread to avoid race conditions and crashes.
Attempts:
2 left
💡 Hint
Think about thread safety and UI frameworks.
🔧 Debug
advanced
2:00remaining
What error occurs with this code missing @MainActor?
This Swift code updates UI state without @MainActor. What error will it cause?
Swift
class ViewModel {
    var text = ""

    func updateText() {
        text = "Hello"
        print(text)
    }
}

let vm = ViewModel()
Task {
    vm.updateText()
}
ANo error; code runs fine
BCompilation error: 'updateText' must be async
CRuntime warning about UI updates off main thread
DDeadlock because of missing await
Attempts:
2 left
💡 Hint
UI updates must happen on the main thread.
📝 Syntax
advanced
1:30remaining
Which option correctly applies @MainActor to a Swift function?
Choose the correct syntax to mark a Swift function to run on the main actor.
A@MainActor func updateUI() { print("UI updated") }
Bfunc @MainActor updateUI() { print("UI updated") }
Cfunc updateUI() { @MainActor print("UI updated") }
Dfunc updateUI() @MainActor { print("UI updated") }
Attempts:
2 left
💡 Hint
Attributes come before the function keyword.
🚀 Application
expert
2:30remaining
How many times is the UI updated in this Swift code?
Given this Swift code using @MainActor and async calls, how many times will the UI update print statement run?
Swift
import Foundation

@MainActor
class UIHandler {
    var updates = 0

    func updateUI() {
        updates += 1
        print("UI updated \(updates) times")
    }
}

let handler = UIHandler()

Task {
    await handler.updateUI()
    Task {
        await handler.updateUI()
    }
    await handler.updateUI()
}
A2 times
B3 times
C1 time
D4 times
Attempts:
2 left
💡 Hint
Count each awaited call to updateUI inside the tasks.