I was making a shop but to show which background is selected I stumbled into a weird hick up. Is there a way to change multiple variables in a loop so that for example:
var achtergrondSelected1:Int = 1
var achtergrondSelected2:Int = 0
var achtergrondSelected3:Int = 0
var achtergrondSelected4:Int = 0
var achtergrondSelected5:Int = 0
var achtergrondSelected6:Int = 0
(Instead of a boolean my old code used 1 and 0, same effect) Let's say the person selects background (= achtergrond) 4, at the moment I'm changing all values manually, but is there a way to create a loop that uses the number in the name to change the values to 0 who need to be 0 and 1 who need to be one?
CodePudding user response:
You can avoid to declare N variables and loops, you can just use an enum.
enum achtergrondSelected : Int
{
case achterground1
case achterground2
case achterground3
case achterground4
case achterground5
}
var selectedBackground : achtergrondSelected
selectedBackground = .achterground1
print(selectedBackground.rawValue)
For cases name I choose "achterground" like it was in your example, but you can obviously choose other names. I hope that's what you was looking for.