I can't seem to find any information on this, anywhere. So I'm learning SwiftUI, and I'm trying to add a Picker to a Form, but the picker won't display the available options when tapped. I don't really know what else to tell you guys so could someone take a look and help me fix this? thanks :)
//Outside the body code
@State var contacts: [String] = [""]
@State var types: [Type] = [.mobile]
//Inside the body code and inside a Form{}
Section(header: Text("Contact")){
ForEach(0..<types.count, id: \.self) { i in
HStack{
Picker(selection: $types[i], label: Text(""), content: {
Text("Mobile").tag(Type.mobile)
Text("Land Line").tag(Type.landline)
Text("Email").tag(Type.email)
}).pickerStyle(DefaultPickerStyle())
.frame(width: 80.0)
TextField("Insert Detail", text: $contacts[i])
}
}
Button("Add Another") {
types.append(.mobile)
contacts.append("")
}
ForEach(_:content:)should only be used for constant data. Instead conform data toIdentifiableor useForEach(_:id:content:)and provide an explicitid!. This is the message printed on console when I tested your code, and it’s clear what went wrong.ForEach(0..<types.count, id:\.self)and make sure each enum case in unique in types array.iwhile specifying the ID explicitly?