Home > Net >  How to access and manipulate values from array of structs
How to access and manipulate values from array of structs

Time:08-17

I'm building an absolutely simple app: a list of restaurants a button that will randomly choose one of them by highlighting it (changed the opacity of the rest elements to 0.1). With the UI part, everything is ok, but can't manage the second part – pick a random element and change opacity for the rest restaurants.

Data: Restaraunts.json – a file that contains restaurants (id, name, photo, isChosen (bool)). Struct (Hashable, Codable, Identifiable) Parse this json and create an array that holds this structures:

var restarauntsArray: [RestarauntsStruct] = load("Restaraunts.json")

UI: Basic UI is working, I have a grid of my restaraunts, and then it's time to create an action for a button: generate random number –> go through all elements in the array of structs and check if the id of element is equal to a random number –> if not, change the bool value for "isVisible" property. I tried I think 10 different approaches and didn't found a solution for that, each time a receive some error and can't find a solution. My current approach:

Button {
        randomChosen = Int.random(in: 0..<3) //so far I have 3 restaraunts
        buttonAction.toggle() // changed the bool value to change the name of the button later 
        for tempVar in restarauntsArray {
            if restarauntsArray.map { $0.id} == randomChosen {
            }
        }
    } label: {
        Text("Where are we going to eat?")
    }

Even without future code I receive an error:

Binary operator '==' cannot be applied to operands of type '[Int]' and 'Int'

And I give up. I don't understand why Int from an array with structures can't be matched with Int from a random variable. Could someone help to fix and explain this part?

CodePudding user response:

restarauntsArray.map { $0.id} returns an array of Int, you cannot compare it to a singleInt, that's what the error is telling you.

Actually you don't have 3 restaurants, you have restarauntsArray.count restaurants

You can do

for restaurant in restarauntsArray {
    if restaurant.id == randomChosen {
        // do something
        break
    }
}

but this is pretty cumbersome. More convenient is

let randomRestaurant = restarauntsArray.randomElement()

And please consolidate the spelling of restar…whatever

CodePudding user response:

You got this error:

Binary operator '==' cannot be applied to operands of type '[Int]' and 'Int'

Because you are trying to compare two different types of value. The random value is an Int value, so the value that you want to use to compare with it or the id of your RestarauntsStruct must be an Int too.

I assume the id inside your RestarauntsStruct is not an Int type too, so you can't use the == operator here.

Below is a sample working code:

struct Something: Identifiable {
  var id: Int
}
struct SampleView: View {
  let arrayOfSomething = [Something(id: 1), Something(id: 2),   Something(id: 3)]
  @State var result = "No Result Yet"
  var body: some View {
    VStack {
        Text(result)
        Button("Do something") {
            let random = Int.random(in: 1...9)
            for sub in arrayOfSomething {
                if sub.id == random {
                    result = String(sub.id)
                }
            }
        }
    }
  }
}
  • Related