We use List with ForEach to show a list of items on the screen. It helps us display many things easily by repeating a view for each item.
Showing a list of names in a contacts app.
Displaying a menu with many options.
Listing tasks in a to-do app.
Showing photos or messages in a feed.
Any time you want to show many similar items in a scrollable list.
Syntax
iOS Swift
List {
ForEach(items, id: \.self) { item in
Text(item)
}
}
items is your data array.
id: \.self tells SwiftUI how to identify each item uniquely.
Examples
Shows a list of fruit names.
iOS Swift
let fruits = ["Apple", "Banana", "Cherry"]
List {
ForEach(fruits, id: \.self) { fruit in
Text(fruit)
}
}
Shows a list of numbers with a label.
iOS Swift
let numbers = [1, 2, 3]
List {
ForEach(numbers, id: \.self) { number in
Text("Number: \(number)")
}
}
Shows an empty list (no rows).
iOS Swift
let emptyList: [String] = []
List {
ForEach(emptyList, id: \.self) { item in
Text(item)
}
}
Shows a list with only one item.
iOS Swift
let singleItem = ["Only one"]
List {
ForEach(singleItem, id: \.self) { item in
Text(item)
}
}
Sample App
This app shows a list of animal names inside a navigation view with a title.
iOS Swift
import SwiftUI
struct ContentView: View {
let animals = ["Dog", "Cat", "Rabbit"]
var body: some View {
NavigationView {
List {
ForEach(animals, id: \.self) { animal in
Text(animal)
}
}
.navigationTitle("Animals")
}
}
}
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
OutputSuccess
Important Notes
Time complexity: Displaying the list is O(n) where n is the number of items.
Space complexity: Depends on the number of items and views created.
Common mistake: Forgetting to provide a unique id for each item causes errors.
Use List with ForEach when you want a scrollable list that can handle dynamic data.
Summary
List with ForEach helps show many items easily.
Always provide a unique id for each item.
Works well for menus, contacts, tasks, and more.
Practice
(1/5)
1. What is the main purpose of using ForEach inside a SwiftUI List?
easy
A. To style the list items
B. To display multiple views by looping over a collection
C. To handle user input events
D. To create a single static view
Solution
Step 1: Understand ForEach role
ForEach loops over a collection to create views for each item.
Step 2: Compare options
Only To display multiple views by looping over a collection describes this looping and displaying behavior correctly.
Final Answer:
To display multiple views by looping over a collection -> Option B
Quick Check:
ForEach = Loop to display views [OK]
Hint: ForEach repeats views for each item in a list [OK]
Common Mistakes:
Thinking ForEach creates only one view
Confusing ForEach with event handling
Assuming ForEach styles views
2. Which of the following is the correct syntax to create a List with ForEach over an array items of strings in SwiftUI?
easy
A. List { ForEach(items, id: \.index) { item in Text(item) } }
B. List { ForEach(items) { item in Text(item) } }
C. List { ForEach(items, id: \.id) { item in Text(item) } }
D. List { ForEach(items, id: \.self) { item in Text(item) } }
Solution
Step 1: Check ForEach syntax
When iterating over a simple array of strings, use id: \.self to identify each item uniquely.
Step 2: Validate options
List { ForEach(items, id: \.self) { item in Text(item) } } uses correct syntax with id: \.self. List { ForEach(items) { item in Text(item) } } misses the id parameter, causing errors. Options C and D use invalid keys for strings.
Final Answer:
List { ForEach(items, id: \.self) { item in Text(item) } } -> Option D
Quick Check:
ForEach with id: \.self for string arrays [OK]
Hint: Use id: \.self for arrays of simple values [OK]
Common Mistakes:
Omitting the id parameter causes errors
Using invalid keys for id on simple arrays
Confusing id: \.self with id: \.id
3. What will be displayed by this SwiftUI code?
let fruits = ["Apple", "Banana", "Cherry"]
List {
ForEach(fruits, id: \.self) { fruit in
Text(fruit)
}
}
medium
A. A list showing Apple, Banana, Cherry as text rows
B. A list showing numbers 0, 1, 2
C. An empty list with no rows
D. A compile-time error due to missing id
Solution
Step 1: Understand the data and ForEach
The array fruits contains three strings. ForEach loops over them with id: \.self.
Step 2: Predict the UI output
Each string is shown as a Text view inside the list, so the list shows Apple, Banana, and Cherry as rows.
Final Answer:
A list showing Apple, Banana, Cherry as text rows -> Option A
Quick Check:
ForEach over strings shows each string as a row [OK]
Hint: ForEach shows each item as a row in List [OK]
Common Mistakes:
Expecting numbers instead of strings
Thinking list will be empty
Assuming id is missing causes error here
4. Identify the error in this SwiftUI code snippet:
struct ContentView: View {
let numbers = [1, 2, 3]
var body: some View {
List {
ForEach(numbers) { num in
Text(\"Number: \(num)\")
}
}
}
}
medium
A. Missing id parameter in ForEach causes a compile error
B. Text view syntax is incorrect
C. List cannot contain ForEach
D. numbers array must be @State
Solution
Step 1: Check ForEach usage with array of Int
ForEach over an array of Int requires an id parameter because Int does not conform to Identifiable.
Step 2: Identify the error
The code misses id: \.self in ForEach, causing a compile-time error.
Final Answer:
Missing id parameter in ForEach causes a compile error -> Option A
Quick Check:
ForEach over Int array needs id: \.self [OK]
Hint: Add id: \.self when ForEach uses non-Identifiable items [OK]
Common Mistakes:
Assuming ForEach works without id for Int arrays
Thinking Text syntax is wrong
Believing List can't contain ForEach
5. You have a list of tasks represented as structs with id and name. How do you correctly display them in a SwiftUI List using ForEach?
hard
A. List { ForEach(tasks, id: \.index) { task in Text(task.name) } }
B. List { ForEach(tasks, id: \.name) { task in Text(task.name) } }
C. List { ForEach(tasks) { task in Text(task.name) } }
D. List { ForEach(tasks, id: \.self) { task in Text(task.name) } }
Solution
Step 1: Understand Identifiable protocol
If the task struct has a unique id property and conforms to Identifiable, ForEach can use the array without specifying id.
Step 2: Analyze options
List { ForEach(tasks) { task in Text(task.name) } } uses ForEach(tasks) without id, which works if tasks conform to Identifiable. Options A, C, and D specify id incorrectly or unnecessarily.
Final Answer:
List { ForEach(tasks) { task in Text(task.name) } } -> Option C
Quick Check:
Use ForEach without id if items are Identifiable [OK]
Hint: If struct is Identifiable, omit id in ForEach [OK]
Common Mistakes:
Adding id when struct already conforms to Identifiable
Using wrong property for id
Using id: \.self on structs without Hashable
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.