![](/style/images/good.png)
![](/style/images/bad.png)
What is the difference between List and ForEach in SwiftUI
source link: https://sarunw.com/posts/difference-between-list-foreach/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
You can easily support sarunw.com by checking out this sponsor.
Sponsor sarunw.com and reach thousands of iOS developers.
What is the difference between List and ForEach in SwiftUI
Even though ForEach
and List
have similar syntax, they serve different purposes.
ForEach(contacts, id: \.self) { contact in
Text(contact)
}
List(contacts, id: \.self) { contact in
Text(contact)
}
ForEach
ForEach
is a view that creates an array of views from an underlying collection of data.
You can think of it as a map function that turns an array of data into multiple views.
struct ContentView: View {
let contacts = [
"John",
"Alice",
"Bob",
"Foo",
"Bar"
]
var body: some View {
ForEach(contacts, id: \.self) { contact in
Text(contact)
}
}
}
The above code is equivalent to
struct ContentView: View {
var body: some View {
Text("John")
Text("Alice")
Text("Bob")
Text("Foo")
Text("Bar")
}
}
ForEach
doesn't contain any style. The result is an array of views.
By default, views without any container view will lay out vertically.
![ForEach creates a collection of views from data.](https://d33wubrfki0l68.cloudfront.net/dff206823f8666cbd68d67a6b2998c61cb8ab97b/c5f19/images/swiftui-list-foreach-foreach.png)
ForEach creates a collection of views from data.
List
or UITableView
in UIKit is a complex view structure that compose many features and styles.
Here are some of the List characteristics.
- Display a collection of views as rows in a column.
- Automatically provides scrolling when the content is too large for the current display.
- Many built-in appearance features that you can configure using view modifiers, e.g., list styles, headers, footers, sections, and separators.
- Many built-in user interactions, e.g., selecting, adding, deleting, and reordering.
In brief, List
is a container view that turns a collection of views into a list structure.
struct ContentView: View {
var body: some View {
List {
Text("John")
Text("Alice")
Text("Bob")
Text("Foo")
Text("Bar")
}
}
}
List
turn a collection of text views into a list structure. Each text view becomes a row's content.
As you can see, each row has a line separator and groups into the same section with round corners around them. These are all styles by a List
view.
![List view.](https://d33wubrfki0l68.cloudfront.net/2040092185bb1962d6e685b25d16dcae15e736a1/21207/images/swiftui-list-foreach-list.png)
List view.
Using ForEach in List
Since ForEach
can turn an array of data into a collection of views and List
can turn those views into a list structure.
We can use ForEach
inside a List
view.
struct ListForEachExample: View {
let contacts = [
"John",
"Alice",
"Bob",
"Foo",
"Bar"
]
var body: some View {
List {
ForEach(contacts, id: \.self) { contact in
Text(contact)
}
}
}
}
The result will be the same as our previous example.
List {
Text("John")
Text("Alice")
Text("Bob")
Text("Foo")
Text("Bar")
}
Creating a list from a collection of data is a very common pattern. Having to use ForEach
inside List
every time might be cumbersome.
Luckily, List
has an initializer with the same syntax as ForEach
. So, we can achieve the same result using this initializer.
List(contacts, id: \.self) { contact in
Text(contact)
}
You can easily support sarunw.com by checking out this sponsor.
Sponsor sarunw.com and reach thousands of iOS developers.
Conclusion
ForEach
is a view that creates an array of views from an underlying collection of data. The resulting views can be used within other container views, e.g., VStack
, HStack
, and List
.
List
is a container view that turns a collection of views into a list structure.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK