简体   繁体   中英

SwiftUI - How do I read a string array out of a Identifiable struct

I am trying to loop through the string array that I created in my Identifiable Struct. I can read a single record of the array but when I put it in a foreach loop, I can't loop through all the records. Basically I want to show all the assets that the selected person has. The Foreach in the DetailView Struct is the problem. Please see image and code below.

import SwiftUI

struct Person :Identifiable {
    var id = UUID()
    var name: String
    var assets: [String]
}

extension Person {

    static func all() -> [Person] {

        return [
            Person(name: "John", assets: ["Car1", "Car2"]),
            Person(name: "Peter", assets: ["House1", "Car3"])
        ]
    }
}

var persons = Person.all()

struct DetailView: View {
    var person1: Person

    var body: some View {

        List{
            Text("NAME")
                .bold()
            Text(person1.name)

            Text("ASSETS")
                .bold()
            ForEach(person1) {asset in
                Text(person1.assets[asset1])
            }
        .navigationBarTitle(Text(person1.name))
        }
    }
}

struct ContentView: View {
    var body: some View {

        NavigationView{
            List{
                ForEach(persons) {person in
                    NavigationLink(destination: DetailView(person1: person)) {
                        Text(person.name)
                    }
                }
            }
        .navigationBarTitle("Person assets")
        .border(Color.black, width: 1)
        }
    }
}

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

try this

import SwiftUI

struct Asset : Identifiable, Hashable {
    var id = UUID()
    var text : String

    init(_ text : String) {
        self.text = text
    }
}

struct Person :Identifiable {
    var id = UUID()
    var name: String
    var assets: [Asset]
}

extension Person {

    static func all() -> [Person] {

        return [
            Person(name: "John", assets: [Asset("Car1"), Asset("Car2")]),
            Person(name: "Peter", assets: [Asset("House1"), Asset("Car3")])
        ]
    }
}

var persons = Person.all()

struct DetailView: View {
    var person1: Person

    var body: some View {

        VStack {
            Text("NAME")
                .bold()
            Text(person1.name)

            Text("ASSETS")
                .bold()
            List (person1.assets, id: \.self) { asset in

                Text(asset.text)
            }
            .navigationBarTitle(Text(person1.name))
        }
    }
}

struct ContentView: View {
    var body: some View {

        NavigationView{
            List{
                ForEach(persons) {person in
                    NavigationLink(destination: DetailView(person1: person)) {
                        Text(person.name)
                    }
                }
            }
            .navigationBarTitle("Person assets")
            .border(Color.black, width: 1)
        }
    }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM