I want to create an enum-like set of strings in a way that will also provide me the ability to order them by their ordinal value and NOT by alphabetic value. for example:
“Extremely Low” = 0
“Very Low” = 1
“Low” = 2
“Medium = 3
“High” = 4
and so on.
I know that in Java and C# for example, enum types can have a value besides their ordinal value.
Is there any way to achieve the same goal in Objective-C?
Thanks!
Just keep the strings for I/O and use an enum for all your computations. For example, define your enum:
You can use this enum in
switchstatements etc. Now you can use anNSArrayor just a simple C-array to map from enum literals to strings:Going from the string version to the enum is more involved, you can use the same
RatingToStringarray and do appropriate comparisons (case-insensitive maybe, fuzzy etc.). You could also use anNSDictionary:Now a lookup will do an exact match on your string and return a
Ratingwrapped as anNSNumber.