Let's say I have a public enum, each of which has multiple associated values. I currently have them stored as such:
public enum foo {
case A
case B
case C
case ... //goes on for hundreds of enums
}
To access a value, I do a linear search using a switch statement as such (within the enum):
func getNumber() -> Int { //multiple functions just like this for each A, B, C...
switch self {
case .A: return 102 //numbers are arbitrary
case .B: return 438
case .C: return 293
case ... //and so on
}
}
func getName() -> String {
switch self {
case .A: return "Apple" //names are arbitrary as well...
case .B: return "Banana"
case .C: return "Cherry"
case ...
}
}
func ...
However, to access something at the bottom of the list, I have to iterate through every single value of the enum, which has to be done multiple times a second. I feel as if this is very inefficient. Ideally, I'd like to be able to:
Store multiple elements, each with their own individual values. Each element should have multiple similar variables (name, weight, size, etc.) associated with it, just with different values for each.
Be able to access each enum without having to iterate through a switch statement. For example, if a user inputs foo.B.number(), ideally it would return "438" without having to go through each element.
NOT subject to change (B's "number" will never be modified, etc.). These values are FINAL.
Be able to access a certain enum with a String. For example, if "Apple" is associated with foo's "A", then a function as such can be called:
func getFoo(s:String) -> foo { ... }
getFoo(s:"Apple") //should return A
What could I do to store and access these?