I need a simple structure to store polygon names based on sides … so for e.g. 1 side is “monogon”, 2 sides is “digon”, 3 sides is “triangle” and so on (say for upto 12 sides)
What is the simplest way to store these and reuse them in the code dynamically? for e.g. if my polygonShape class has 3 as the number of sides, it should return “Triangle” as the name (which is a property declared in the class) ( I am using Obj-c). I thought of 3 options
1. enums
typedef enum {monogon = 1, digon, triangle, ...}
But then realized this is reverse of what I need. They would actually encode the numbers for me to a string. I need to obtain the names from the numbers.
2. switch-case statements – came as close alternative
3. Arrays – Then I thought may be use arrays and their indexes map to Strings
Somehow I feel I might be missing something in the “too-simple-to-be-true” solution of arrays.
Any opinions appreciated.
I think an array would be the way to go. Only catch, is that an array starts at 0 so you’d have to store something like an empty string in index zero, or always subtract 1 from you number of sides to get the proper index. You can’t store nil in index zero since it is used to mark the end of the array. The other approach could be a NSDictionary so you don’t have to deal with index zero. You would simply have NSStrings 1 – x as your keys, and then the NSStrings of mongon, digon, triangle,… as your objects in the array. Hope this helps.