NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

How to create a list of dynamic items

Paul Hudson    @twostraws   

Updated for Xcode 14.2

In order to handle dynamic items, you must first tell SwiftUI how it can identify which item is which. This is can either be done by specifying the identifying property by hand, or by using the Identifiable protocol. This protocol has only one requirement, which is that the type must have some sort of id value SwiftUI can use to see which item is which.

To demonstrate this, we can create three things then put them together:

  1. A Restaurant struct that says restaurants have an ID and name, with the ID being a random identifier just so that SwiftUI knows which is which.
  2. A small view that determines what each row in our list looks like. In our case we’re going to define a RestaurantRow view that stores one restaurant and prints its name in a text view.
  3. A list view that shows several restaurants. This means creating some example data, putting it into an array, then passing that into a list to be rendered.

Here’s all that in code:

// A struct to store exactly one restaurant's data.
struct Restaurant: Identifiable {
    let id = UUID()
    let name: String
}

// A view that shows the data for one Restaurant.
struct RestaurantRow: View {
    var restaurant: Restaurant

    var body: some View {
        Text("Come and eat at \(restaurant.name)")
    }
}

// Create three restaurants, then show them in a list.
struct ContentView: View {
    let restaurants = [
        Restaurant(name: "Joe's Original"),
        Restaurant(name: "The Real Joe's Original"),
        Restaurant(name: "Original Joe's")
    ]

    var body: some View {
        List(restaurants) { restaurant in
            RestaurantRow(restaurant: restaurant)
        }
    }
}

Download this as an Xcode project

A list containing “Come and eat at Joe's Original”, “Come and eat at The Real Joe's Original”, and “Come and eat at Original Joe’s”.

Most of that is just creating data – the last part is where the real action is: using List(restaurants) creates a list from the restaurants array, executing the following closure once for every item in the array. Each time the closure goes around the restaurant input will be filled with one item from the array, so we use that to create a RestaurantRow.

In fact, in trivial cases like this one we can make the code even shorter: List(restaurants, rowContent: RestaurantRow.init) does exactly the same thing.

Hacking with Swift is sponsored by Stream

SPONSORED Build a functional Twitter clone using APIs and SwiftUI with Stream's 7-part tutorial series. In just four days, learn how to create your own Twitter using Stream Chat, Algolia, 100ms, Mux, and RevenueCat.

Try now!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Similar solutions…

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.7/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.