Complete the code to create a SwiftUI List that shows items from an array.
List([1]) { item in Text(item) }
The List needs a data source array to show dynamic content. Here, items is the array.
Complete the code to make the List update when the data changes.
@State private var fruits = ["Apple", "Banana", "Cherry"] var body: some View { List([1]) { fruit in Text(fruit) } }
self. inside the List parameter causes errors.@fruits is invalid syntax here.The List uses the @State variable fruits to show dynamic content that updates automatically.
Fix the error in the List code by completing the blank with the correct identifier for unique items.
List(fruits, id: [1]) { fruit in Text(fruit) }
id or self without the backslash causes errors.fruit is invalid here.Using id: \.self tells SwiftUI to use the item itself as a unique identifier, which is needed for simple types like String.
Fill both blanks to create a List that shows numbers from 1 to 5 with their squares.
let numbers = Array(1...5) List(numbers, id: [1]) { num in Text("\(num) squared is \(num [2] num)") }
+ instead of * changes the meaning.num as id without backslash causes errors.The List uses \.self as id for unique numbers. The square is calculated by multiplying num * num.
Fill all three blanks to create a dynamic List showing names and ages from a dictionary.
let people = ["Alice": 30, "Bob": 25, "Carol": 27] List(people.keys.sorted(), id: [1]) { name in Text(name) Text("Age: \(people[[2]] ?? 0)") Text("Name length: \([3].count)") }
people as id causes errors.The List uses \.self as id for unique names. The age is accessed by people[name]. The length of the name is name.count.