Learn Courses My Dashboard

Creating unique binding values in sliders for multiple objects


I am trying to create a view in which the user has selected multiple objects and for each of those selected objects (in this case, “Chair”, “Desk”, and “Table”), as the screen appears, there are 2 sliders where they are able to adjust binding values for 2 different properties, as shown below.


The problem I am trying to work out is how to dynamically set these binding values for each respective object, because when I declare for example @State var sellPrice and bind that to the sliders as Slider(value: $sellPrice, obviously every “sell price” slider is going to move together as I move one of them. Given the code below, how would you suggest I structure these bindings to be unique to each object?

@Environment(.dismiss) var dismiss
@Environment(.managedObjectContext) private var viewContext

    entity: Items.entity(),
    sortDescriptors: [NSSortDescriptor(keyPath: \Items.itemName, ascending: true)]
var items: FetchedResults<Items>

    entity: Office.entity(),
    sortDescriptors: [NSSortDescriptor(keyPath: \Office.officeName, ascending: true)]
var officeArray: FetchedResults<Office>

@Binding var editOffice: String

@State var sellPrice: Double = 500

@State var cost: Double = 200

var body: some View {
    VStack(alignment: .leading) {
        Text("Enter Item Info")
        Form {
            List {
                //Group {
                ForEach(officeArray.filter {$0.wrappedOfficeName == editOffice}) { office in

                    ForEach(office.itemArray, id: \.self) {itemList in
                        Text(getItemName(id: itemList))
                                .font(Font.custom("Avenir Heavy", size: 14))
                        Text("Sell Price: \(sellPrice, specifier: "%.0f")")
                        Slider(value: $sellPrice, in: 0.0...3500.0, step: 1.0, onEditingChanged: {_ in

                        Text("Cost: \(cost, specifier: "%.0f")")
                        Slider(value: $cost, in: 0.0...3500.0, step: 1.0, onEditingChanged: {_ in




Create a state property for each slider

But the number of total objects being selected is unknown and is dependent on what the user selects, for example in this case the user selected Chair, Desk, and Table. I am trying to figure out how to code it dynamically so that there is a unique sell price and cost slider for each object regardless of how many objects they choose.

Create an ObservableObject that has as an @Published property an array of a struct to hold information about each item (sell price, cost, etc).

I’ll let someone else flesh this out because I don’t have the time to write up something more in depth right now. If nobody has gotten around to it by tomorrow, I’ll throw something up here.

You can also try to tackle it yourself!

Hey @roosterboy - I’ve been trying to work this out but no luck. Would you or someone else on the team be able to write up what the code for the object would look like?


Google and in this case, Youtube’s your friend.

also check out link below for property wrappers