Lefora Free Forum
Loading
981 views

special enumeration definition

Page 1
1–9
novice - member
27 posts

Is it possible to define an Enum as such? 

enum languages {
0 = "English",
1 = "Spanish",
2 = "Latin"
}

?
63 posts

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…

__________________
kompilesoft

come and see my website! it's real cool reviews
?
63 posts

So really, that could be:

typedef enum _langues {
english,
spanish,
latin
} languages;

I just realized that. surprise

__________________
kompilesoft

come and see my website! it's real cool reviews
novice - member
27 posts

Thanks for the reply, but its actually not the same thing. I guess I am stuck with an array!

?
288 posts

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.

__________________
?
63 posts


Or a dictionary?

typedef enum _someStuff { english, spanish, latin } SomeStuff;
NSDictionary* _langs = [NSDictionary dictionaryWithObjectsAndKeys: @"English", english, @"Spanish", spanish];

__________________
kompilesoft

come and see my website! it's real cool reviews
?
288 posts



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.
 

It is of course possible to use the C++ templates in Obj/C.  When you put your code into a file with the extension .mm (example: foo.mm), the compiler will accept C++ (and Obj/C) at the same time.

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.




__________________
?
63 posts

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.

__________________
kompilesoft

come and see my website! it's real cool reviews
?
63 posts




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.

__________________
kompilesoft

come and see my website! it's real cool reviews
Page 1
1–9

Locked Topic


You must be a member to post in this forum

Join Now!