Identifiableに適合していないStructでListを使う


Identifiableに適合していないStructでListを使う方法です。 id でプロパティユニークとなるプロパティを指定することで List で使うことができます。

ContentView.swift

import SwiftUI

struct Pokemon {
    let name: String
    let type: String
}

struct ContentView: View {
    let pokemons = [Pokemon(name: "Snorlax", type: "Normal"), Pokemon(name: "Eevee", type: "Normal")]
    var body: some View {
        List(pokemons, id: \.name) { pokemon in
            Text(pokemon.name)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}