Search code examples
iosswiftswiftuiswiftui-picker

getting error !! Picker: the selection "nil" is invalid and does not have an associated tag, this will give undefined results


this is my main file getting error in picker not able to select the value from picker getting error in first picker

import SwiftUI

struct MainView: View { 
    @ObservedObject private var branchSelectionViewModel = BranchSelectionViewModel() 
    @StateObject private var semesterSelectionViewModel = SemesterSelectionViewModel() 
    @StateObject private var subjectListViewModel = SubjectListViewModel() 
    @StateObject private var paperListViewModel = PaperListViewModel()

var body: some View {
    NavigationView {
        ScrollView {
            VStack {
                Picker("Select Branch", selection: $branchSelectionViewModel.selectedBranch) {
                    ForEach(branchSelectionViewModel.branches, id: \.id) { branch in
                        Text(branch.name).tag(branch)
                    }
                }
                .pickerStyle(.menu)
                
                
                
                Picker("Select Semester", selection: $semesterSelectionViewModel.selectedSemester) {
                    ForEach(semesterSelectionViewModel.semesters, id: \.id) { semester in
                        Text(semester.name).tag(semester)
                    }
                }
                .pickerStyle(.wheel)
                
                if let selectedBranch = branchSelectionViewModel.selectedBranch,
                   let selectedSemester = semesterSelectionViewModel.selectedSemester {
                    SubjectListView(viewModel: subjectListViewModel,
                                    branchId: selectedBranch.id,
                                    semesterId: selectedSemester.id)
                }
            }
        }
        .onAppear {
            branchSelectionViewModel.fetchBranches()
        }
        .navigationTitle("Previous Year Papers")
    }
}
}

below code is the BranchSelectionViewModel from which picker is selecting the branch but in terminal getting message "Picker: the selection "nil" is invalid and does not have an associated tag, this will give undefined results"


class BranchSelectionViewModel: ObservableObject {
    @Published var branches: [Branch] = []
    @Published var selectedBranch: Branch?
    
    func fetchBranches() {
        // Simulate fetching branches from a network or database
        branches = [
            Branch(id: 1, name: "Computer Science"),
            Branch(id: 2, name: "Electrical Engineering"),
            // ... add more branches ...
        ]
    }
}

Solution

  • selectedBranch is an optional, it can be nil. But there is no corresponding tag for Nothing selected aka nil.

    A possible solution is to make selectedBranch non-optional with a default value for example of Branch(id: 0, name: "None"). Append new branches to the array and restore the default value when the array becomes empty. Or use the default value only if the array is empty.

    Side note:

    Use always @StateObject for objects owned by the view.
    @ObservedObject is the reference type equivalent of @Binding.