UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Picker Choice To Alter Second Picker

Forums > SwiftUI

I have a picker whose values are from a JSON file.

These are car makers. When you select a maker of car i would like the second picker to show the model of cars that that maker makes.

The structure of the JSON is

      "brand" : "BMW",
        "name" : "335",
        "w" : 0,
        "x" : 0,
        "y" : 0,
        "z" : 0

Thanks very much in advance.

3      

Here's a solution. Not the most elegant, but it works.

JSON structure:

[
    {
        "brand": "BMW",
        "models": [
            {
                "name": "335"
            },
            {
                "name": "x5"
            }
        ]
    },
    {
        "brand": "Tesla",
        "models": [
            {
                "name": "CyberTruck"
            },
            {
                "name": "X"
            },
            {
                "name": "S"
            }
        ]
    },
    {
        "brand": "Porsche",
        "models": [
            {
                "name": "911"
            },
            {
                "name": "Taycan"
            }
        ]
    }
]

Models:

struct Manufacturer: Decodable {
    let brand: String
    let models: [Model]
}

struct Model: Decodable {
    let name: String
}

And the actual view and its model:

struct ContentView: View {

    @ObservedObject private var model = ContentViewModel()

    var body: some View {
        VStack {
            if model.selectedManufacturer == -1 {
                Text("No Brand Selected")
            } else {
                Text("\(model.carData[model.selectedManufacturer].brand)")
            }

            Picker(selection: $model.selectedManufacturer, label: Text("Brand")) {
                Text("None")
                    .tag(-1)

                ForEach(0 ..< model.carData.count) { carIndex in
                    Text(self.model.carData[carIndex].brand)
                        .tag(carIndex)
                }
            }

            if model.selectedManufacturer != -1 {
                Picker(selection: $model.selectedModel, label: Text("Model")) {
                    Text("None")
                        .tag(-1)

                    ForEach(0 ..< model.models.count, id: \.self) { modelIndex in
                        Text(self.model.models[modelIndex].name)
                            .tag(modelIndex)
                    }
                }
            }
        }
    }
}

class ContentViewModel: ObservableObject {
    let carData: [Manufacturer]

    @Published var selectedManufacturer = -1 {
        didSet {
            // reset the currently selected model to "None" when the manufacturer changes
            selectedModel = -1
        }
    }
    @Published var selectedModel = -1
    var models: [Model] {
        if (0 ..< carData.count).contains(selectedManufacturer) {
            return carData[selectedManufacturer].models
        }

        return []
    }

    init() {
        let url = Bundle.main.url(forResource: "TestCars", withExtension: "json")!
        let data = try! Data(contentsOf: url)
        carData = try! JSONDecoder().decode([Manufacturer].self, from: data)
    }
}

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.