I am using in swiftUI. When select picker, it is not changing. Here is code..
Here is datamodel:
struct SourceAccountModel:Codable,Identifiable{
var id: Int
let accountNumber: String
let accountTitle: String
let priaryAccount: String
init(id:Int=0,accountNumber: String, accountTitle: String, priaryAccount: String) {
self.id = id
self.accountNumber = accountNumber
self.accountTitle = accountTitle
self.priaryAccount = priaryAccount
}
}
Here is my code
struct Test2: View { @State private var selectedOption = "Option 1" @State private var sourceAccountList = [SourceAccountModel]() var body: some View { VStack{ ZStack { RoundedRectangle(cornerRadius: 8) .fill(Color.white) .shadow(radius: 2) Picker(selection: $selectedOption,label: EmptyView()) { ForEach (0..<sourceAccountList.count,id: \.self) { Text(sourceAccountList[$0].accountNumber) } } .padding(8) } .frame(maxWidth: .infinity) }.onAppear{ intitializeValue() } } func intitializeValue(){ self.sourceAccountList.append(SourceAccountModel(id:1,accountNumber: "Option 1", accountTitle: "", priaryAccount: "")) self.sourceAccountList.append(SourceAccountModel(id:2,accountNumber: "Option 2", accountTitle: "", priaryAccount: "")) } }
Always select first value. What is the wrong with my code?