Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add a pull-to-refresh modifier to the List.
iOS Swift
List(items, id: \.self) { item in
Text(item)
}
.[1] {
print("Refreshing data...")
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using onAppear instead of refreshable.
Trying to add pull-to-refresh without the refreshable modifier.
✗ Incorrect
The .refreshable modifier adds pull-to-refresh behavior to a List in SwiftUI.
2fill in blank
mediumComplete the async closure to simulate data refresh with a 2-second delay.
iOS Swift
.refreshable {
await Task.sleep(UInt64([1] * 1_000_000_000))
print("Data refreshed")
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 2 without converting to nanoseconds.
Using 2000000000 without multiplying by UInt64.
✗ Incorrect
Task.sleep expects nanoseconds, so 2 seconds is 2 * 1_000_000_000 nanoseconds.
3fill in blank
hardFix the error by completing the code to update the items array after refresh.
iOS Swift
.refreshable {
await Task.sleep(2_000_000_000)
[1] = ["Apple", "Banana", "Cherry"]
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using var or let inside the closure which causes redeclaration errors.
Not using self when required inside closures.
✗ Incorrect
Inside closures, use self.items to update the state variable properly.
4fill in blank
hardFill both blanks to declare the items array and bind it to the List.
iOS Swift
@State private var [1] = ["Orange", "Grape"] var body: some View { List([2], id: \.self) { fruit in Text(fruit) } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the state variable and List data source.
Forgetting to mark the variable with @State.
✗ Incorrect
The @State variable is named items and the List uses items as its data source.
5fill in blank
hardFill all three blanks to create a refreshable List that updates items after a delay.
iOS Swift
@State private var [1] = ["Cat", "Dog"] var body: some View { List([2], id: \.self) { pet in Text(pet) } .refreshable { await Task.sleep([3]) self.[1] = ["Cat", "Dog", "Rabbit"] } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the state and List data source.
Using 1_000_000_000 for 2 seconds delay.
✗ Incorrect
The state variable and List data source are 'items'. The delay is 2 seconds in nanoseconds.