0
0
iOS Swiftmobile~10 mins

MainActor for UI updates in iOS Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to ensure the UI update runs on the main thread using MainActor.

iOS Swift
func updateLabel() {
  Task {
    await [1] {
      label.text = "Hello, world!"
    }
  }
}
Drag options to blanks, or click blank then click option'
AMainActor.run
BDispatchQueue.global().async
CTask.sleep
DDispatchQueue.main.asyncAfter
Attempts:
3 left
💡 Hint
Common Mistakes
Using background queues like DispatchQueue.global() causes UI update errors.
Forgetting to use await with MainActor.run.
2fill in blank
medium

Complete the function declaration to mark it as running on the main actor.

iOS Swift
@[1]
func refreshUI() {
  label.text = "Updated"
}
Drag options to blanks, or click blank then click option'
AGlobalActor
BUIActor
CMainActor
DBackgroundActor
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect actor names like @GlobalActor or @BackgroundActor.
Omitting the @ symbol.
3fill in blank
hard

Fix the error in this async function to update UI safely on the main thread.

iOS Swift
func loadData() async {
  let data = await fetchData()
  await [1] {
    label.text = data
  }
}
Drag options to blanks, or click blank then click option'
ADispatchQueue.global().async
BDispatchQueue.main.sync
CTask.detached
DMainActor.run
Attempts:
3 left
💡 Hint
Common Mistakes
Using background queues for UI updates.
Using synchronous dispatch on main queue inside async code.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that filters keys with length greater than 3 and converts keys to uppercase.

iOS Swift
let filteredDict = Dictionary(uniqueKeysWithValues: data.compactMap { key, value in
  guard key.count [1] 3 else { return nil }
  return (key.[2](), value)
})
Drag options to blanks, or click blank then click option'
A>
Blowercased
Cuppercased
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > in the guard condition.
Using lowercased() instead of uppercased() for keys.
5fill in blank
hard

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.

iOS Swift
let result = Dictionary(uniqueKeysWithValues: items.compactMap { key, value in
  guard value [1] 10 else { return nil }
  return (key.[2](), [3])
})
Drag options to blanks, or click blank then click option'
A<
B>
Cvalue
Duppercased
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > in the guard.
Swapping key and value in the return tuple.