Na el
Na el

Reputation: 67

how to insert a structured identifiable array in a list?

struct Gg: Identifiable{
    
    let id: Int
    let task: String
}

struct ContentView: View {

@State private var items = [Gg(id: 1, task:"take the trash out"), Gg(id: 2, task:"Go for a run")]


var body: some View {
        
        NavigationView {
           
            ZStack(alignment: .center) {
                VStack {
                    List(self.items, id: \.self) { index in
                        Text("\(index)")
                    }

}}

I get the following error

Cannot declare entity named '$id'; the '$' prefix is reserved for implicitly-synthesized declarations Initializer 'init(_:id:rowContent:)' requires that 'Gg' conform to 'Hashable'

quit new and starting out appreciate the help

Upvotes: 1

Views: 74

Answers (1)

Asperi
Asperi

Reputation: 257711

You iterate by item, not by indices, so it is simply

            VStack {
                List(self.items) { item in
                    Text(item.task)
                }

Upvotes: 1

Related Questions