Friendtam
Friendtam

Reputation: 331

Using enum datatype in Objective C

I have declared an enum datatype like:

typedef enum TagTypes
{
   BUTTON_TAG_1 = 1,
   BUTTON_TAG_2,
   BUTTON_TAG_3,
   NEW_TAG

}ButtonTag;

typedef enum TagType
{
   LABEL_TAG_1 = 1,
   LABEL_TAG_2,
   NEW_TAG
}LabelTag;

I wanted to find the corresponding tag of the button or label through this tag value as

(clickedbutton.tag == ButtonTag.BUTTON_TAG1) or (changingLabel.tag == LabelTag.LABEL_TAG_1)

but this syntax doesn't seem to be possible in Obj C, it throws me error saying Expected Identifier or ")"

Is there a way i can select tagNames by specifying tagDatatype like:

LabelTag.LABEL_TAG_2, ButtonTag.BUTTON_TAG2, ...

Thanks for any help

clickedbutton.tag == BUTTON_TAG1 will work, but I prefer to use it like tagName.tagValue , so that i can have same tagValues for different tag sets say tagValue "NEW_TAG" in both LabelTag and ButtonTag.

Upvotes: 0

Views: 1691

Answers (4)

nielsbot
nielsbot

Reputation: 16031

If you really want to use the form of LabelTag::LABEL_TAG_2 you can use objective-c++ mode (change your file extension to .mm) and do this:

class FirstEnum
{
    public: 
    enum { a, b, c } ;
} ;

class SecondEnum
{
    public:
    enum { a, b, c } ;
} ;

Then in your code can can refer to LabelTag::a for example.

Upvotes: 0

HelmiB
HelmiB

Reputation: 12333

you may use switch-case

switch(LabelTag)
{
   case : LABEL_TAG_1

   break;

   case : LABEL_TAG_2

   break;
}

Upvotes: 0

MrMage
MrMage

Reputation: 7496

Have a look at Apple's headers. They simply prefix all enum entries with the enum's name, e.g. UIViewAnimationCurveEaseInOut in the enum UIViewAnimationCurve. I suggest you do the same.

Upvotes: 0

Jesus Ramos
Jesus Ramos

Reputation: 23266

I believe it follows the same convention as it does in C where you just write

if (clickedbutton.tag == BUTTON_TAG1)

without specifying the enum type. You only have to specify the type when its a variable.

Upvotes: 5

Related Questions