1

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
  
   }
}
  1. 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?

Enamul Haque
  • 4,789
  • 1
  • 37
  • 50

1 Answers1

2

selectedOption is a String, but your ForEach iterates over Range<Int>.

You can fix this by changing selectedOption to Int, e.g.

@State private var selectedOption = 0

You might find it easier to store the actual object in selectedOption: SourceAccountModel, iterate over the sourceAccountList, and tag each row:

struct SourceAccountModel: Identifiable, Hashable {
    let id: Int
    let accountNumber: String
    
    init(id: Int, accountNumber: String) {
        self.id = id
        self.accountNumber = accountNumber
    }
}

struct ContentView: View {
    
    init() {
        let sourceAccountList = [SourceAccountModel(id: 1, accountNumber: "Option 1"),
                                 SourceAccountModel(id: 2, accountNumber: "Option 2")]
        
        _sourceAccountList = State(wrappedValue: sourceAccountList)
        _selectedOption = State(wrappedValue: sourceAccountList[0])
    }
    
    @State private var selectedOption: SourceAccountModel
    @State private var sourceAccountList = [SourceAccountModel]()
    
    var body: some View {
        VStack {
            Picker("Select", selection: $selectedOption) {
                ForEach(sourceAccountList) { model in
                    Text(model.accountNumber).tag(model)
                }
            }
        }
    }
}
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160