List

cilippofilia's avatar
Published Dec 14, 2022
Contribute to Docs

The List view arranges its elements into a column. It can be either static, when hard-coding the data into the list, or dynamic, when the data is pulled from an array.

Syntax

var body: some View {
    List {
        Subviews here
    }
}

Example

In the example below, a List pulls the data from the continents array, and then displays them into a single column:

var continents = ["Africa", "Antartica", "Asia", "Europe", "North America", "Oceania", "South America"]
var body: some View {
List(continents, id: \.self) {
Text($0)
}
}
  • id: \.self allows Swift to identify each of the items uniquely in order to update the values when changed.
  • $0 is a shorthand syntax used to reference the first argument in a closure.

This will display the following:

List

All contributors

Contribute to Docs

Learn SwiftUI on Codecademy