Complete the code to ensure the UI update runs on the main thread using MainActor.
func updateLabel() {
Task {
await [1] {
label.text = "Hello, world!"
}
}
}Using MainActor.run ensures the code inside runs on the main thread, which is required for UI updates.
Complete the function declaration to mark it as running on the main actor.
@[1] func refreshUI() { label.text = "Updated" }
Marking the function with @MainActor ensures it always runs on the main thread for UI safety.
Fix the error in this async function to update UI safely on the main thread.
func loadData() async {
let data = await fetchData()
await [1] {
label.text = data
}
}Using MainActor.run inside async functions ensures UI updates happen on the main thread safely.
Fill both blanks to create a dictionary comprehension that filters keys with length greater than 3 and converts keys to uppercase.
let filteredDict = Dictionary(uniqueKeysWithValues: data.compactMap { key, value in
guard key.count [1] 3 else { return nil }
return (key.[2](), value)
})The guard checks keys longer than 3 characters using >. The keys are converted to uppercase with uppercased().
Fill all three blanks to create a dictionary comprehension that filters values greater than 10, converts keys to uppercase, and uses the value as is.
let result = Dictionary(uniqueKeysWithValues: items.compactMap { key, value in
guard value [1] 10 else { return nil }
return (key.[2](), [3])
})The guard filters values greater than 10 using >. Keys are converted to uppercase with uppercased(). The value is returned as is.