xcodeswiftuiswiftui-picker

SwiftUI 2.0 App crash when Picker selectedValue changed


Here is my sample code:

import SwiftUI

final class ViewModel: ObservableObject {
    @Published var countries: [Country?] = [
        Country(id: 0, name: "country1", cities: ["c1 city1", "c1 city2", "c1 city3"]),
        Country(id: 1, name: "country2", cities: ["c2 city1", "c2 city2", "c2 city3"]),
        Country(id: 2, name: "country3", cities: ["c3 city1", "c3 city2", "c3 city3"])
    ]
}

struct ContentView: View {
    
    @ObservedObject var viewModel = ViewModel()
    @State private var selectedCountry: Country? = nil
    @State private var selectedCity: String? = nil
    
    var body: some View {
        VStack {
            Picker("", selection: $selectedCountry) {
                ForEach(viewModel.countries, id: \.self) { country in
                    Text(country!.name).tag(country)
                }
            }
            .pickerStyle(SegmentedPickerStyle())
            Text(selectedCountry?.name ?? "no selection")
            
            if selectedCountry != nil {
                Picker("", selection: $selectedCity) {
                    ForEach(selectedCountry!.cities, id: \.self) { city in
                        Text(city!).tag(city)
                    }
                }
                .pickerStyle(SegmentedPickerStyle())
                Text(selectedCity ?? "no selection")
            }
        }
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

struct Country: Codable, Hashable, Identifiable {
    var id: Int
    var name: String
    var cities: [String?]
}

It works at first but when select a country then select another country then go back to the first choice it crashes, I am using latest Xcode beta I don't know if it is the cause or my approach is wrong.


Solution

  • The problem is in cached binding. We need to recreate picker if source of data changed.

    Find below a fix. Tested with Xcode 12.4 / iOS 14.4

    if selectedCountry != nil {
        Picker("", selection: $selectedCity) {
            ForEach(selectedCountry!.cities, id: \.self) { city in
                Text(city!).tag(city)
            }
        }
        .pickerStyle(SegmentedPickerStyle())
        .id(selectedCountry!)                // << here !!
    
        Text(selectedCity ?? "no selection")
    }