special enumeration definition
How about this:
typedef enum _languages {
english = 0,
spanish = 1,
latin = 2,
// ......
} languages;
That's a typical approach to enums. Although by default, as in an array, the first object is 0, the second 1, the third 2…
So really, that could be:
typedef enum _langues {
english,
spanish,
latin
} languages;
I just realized that.
Thanks for the reply, but its actually not the same thing. I guess I am stuck with an array!
Well, I think you're both right. K is correct - an enum enables you to associate a symbol with a number. If you want a string for a number, then I think you'll have to construct and populate an array in memory.
Or a dictionary?
typedef enum _someStuff { english, spanish, latin } SomeStuff;
NSDictionary* _langs = [NSDictionary dictionaryWithObjectsAndKeys: @"English", english, @"Spanish", spanish];
That's interesting. Good thinking K.
I believe NSDictionary only supports NSString as keys - which makes it easy to convert an NSString to a number. Converting a number to a string (using the dictionary) requires a search. The C++ map template can accept (almost) anything as a key or a value - however it has to be homogeneous (all keys have to be of the same type).
You could consider an NSArray to be a dictionary in which the keys are integers, however I believe NSArray may not be sparse. A sparse array is an array in which the indices are not contiguous. I believe the C++ vector template supports this.
The PostScript Language stores code and data in dictionaries and has no restriction on keys (or values) - a key can even be a dictionary (even itself!). Recursive data structures are totally valid. PostScript is a thing of beauty. Obj/C is also magnificent in its elegance and simplicity.
Often, as keys, NSString * const NSFoobar = @"Foo Bar lifting" are defined at the beginning of a file. For example, this contains some constants. So you can declare the stuff as follows.
NSString * const KSSpanish = @"KSSpanish";
NSString * const KSEnglish = @"KSEnglish";
// etc.
NSDictionary* _langs = [NSDictionary dictionaryWithObjectsAndKeys: @"English", KSEnglish, @"Spanish", KSSpanish];
I used the KS prefix to avoid conflict with anything else.
Often, as keys, NSString * const NSFoobar = @"Foo Bar lifting" are defined at the beginning of a file. For example, this contains some constants. So you can declare the stuff as follows.
NSString * const KSSpanish;
NSString * const KSEnglish;
// etc.
NSDictionary* _langs = [NSDictionary dictionaryWithObjectsAndKeys: @"English", KSEnglish, @"Spanish", KSSpanish];
I used the KS prefix to avoid conflict with anything else.
Sorry, I had pressed the wrong button to edit the previous post.