Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

swift - Binding Array of Structs with Strings in an Array of Structs

I'm new to Swift so I hope this isn't something really silly. I'm trying to build an array of Structs, and one of the parameters is another Array with another Struct in it. I'm not sure if there is a better way, but I thought I was making really good progress right up till I tried to edit the embedded Struct. In it's simplified form it looks like this ...

struct Group: Identifiable, Codable {
    var id = UUID()
    var name: String
    var number: Int
    var spaces: Bool
    var businesses: [Business]
}

struct Business: Identifiable, Codable {
    var id = UUID()
    var name: String
    var address: String
    var space: Int
    var enabled: Bool
    
}

These are used in a class with an Observable var that stored in User Defaults

class GroupSettings: ObservableObject {
    
    @Published var groups = [Group]() {
        didSet {
            UserDefaults.standard.set(try? PropertyListEncoder().encode(groups), forKey: "groups")
        }
    }

    init() {
        if let configData = UserDefaults.standard.value(forKey: "groups") as? Data {
           if let userDefaultConfig = try?
              PropertyListDecoder().decode(Array<Group>.self, from: configData){
                  groups = userDefaultConfig
              }
        }
     }
}

Its passed in to my initial view and then I'm wanting to make an "Edit Detail" screen. When it gets to the edit detail screen, I can display the Business information in a Text display but I can't get it to working a TextField, it complains about can't convert a to a Binding, but the name from the initial Struct works fine, similar issues with the Int ...

I pass a Group from the first view which has the array of Groups in to the detail screen with the @Binding property ...

@Binding var group: Group

var body: some View {

       TextField("", text: $group.name)     <---- WORKS

       List {
           ForEach(self.group.businesses){ business in
           
              if business.enabled {

                 Text(business.name)               <---- WORKS
                 TextField("", business.address)   <---- FAILS
                 TextField("", value: business.space, formatter: NumberFormatter())   <---- FAILS

              } else {

                 Text("(business.name) is disabled"
              }
           }
       }
}

Hopefully I've explained my self well enough, and someone can point out the error of my ways. I did try embedding the 2nd Struct inside the first but that didn't help.

Thanks in advance!

question from:https://stackoverflow.com/questions/65864121/binding-array-of-structs-with-strings-in-an-array-of-structs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could use indices inside the ForEach and then still use $group and accessing the index of the businesses via the index like that...

List {
    ForEach(group.businesses.indices) { index in
        TextField("", text: $group.businesses[index].address)
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...