I am struggling with an issue I think (hopefully) has a simple fix that you may be able to help with.
I am trying to run a ForEach loop over a number of variables. To keep things simple here, I have only included two variables but there are many more, hence why I want to use a ForEach loop rather than have the same code repeated for each variable. The variables are each based on different enums.
Intended outcome
I want to run a ForEach loop in my code that loops through an array of variables and extracts the variable's description and associated rawValue.
Variables
var profileEyeColor: EyeColor = EyeColor()
var profileHairColor: HairColor = HairColor()
Enums
enum EyeColor: String, CaseIterable {
case notSet
case amber
case blue
case brown
case gray
case green
case hazel
case other
case witheld
var description: String {
"Eye Color"
}
init() {
self = .notSet
}
}
enum HairColor: String, CaseIterable {
case notSet
case black
case blond
case brown
case auburn
case red
case gray
case white
case other
case witheld
var description: String {
"Hair Color"
}
init() {
self = .notSet
}
}
ForEach loop
ForEach([profileEyeColor, profileHairColor], id: \.self) { item in
if item != .notSet && item != .witheld {
print(item.description)
print(item.rawValue)
}
}
Actual result
Build fails and xCode errors include:
- Cannot convert value of type 'profileHairColor' to expected element type 'EyeColor'
Alternative attempted
I have tried to run instead using a for loop rather than ForEach. I'm using SwiftUI, so can't implement a for loop within the body.
I've also tried splitting it out into a separate function, but get an error on the function.
func profileDetailsLoop(data: [Any]) -> View {
ForEach(data, id: \.self) { item in
if item != .notSet && item != .witheld {
HStack {
Text(item.description)
Text(item.rawValue)
}
}
}
Error: Value of protocol type 'Any' cannot conform to 'Hashable'; only struct/enum/class types can conform to protocols
If I replace [Any] with [enum] then I get the following error: Expected element type
And if I replace [Any] with any specific enum type, such as [EyeColor] that doesn't work (and presumably also won't won't work on different enums types).