1

i have an enum.

Example:

enum events

{

MOVE_UP = 0,
MOVE_DOWN,
MOVE_RIGHT,
MOVE_LEFT,

};

i want to pass particular enum value as function arguments. for example:

my method definition is

  • (void) invokeEvents:(enum events)events withMessage:(NSDictionary*)message;

while calling this method i am passing arguments like:

[self invokeEvents:MOVE_UP|MOVE_DOWN|MOVE_RIGHT withMessage:message;

while checking the received parameter events is value is always last value of list of MOVE_UP|MOVE_DOWN|MOVE_RIGHT values. MOVE_RIGHT 2 as per enum value.

but i want all the values like "MOVE_UP|MOVE_DOWN|MOVE_RIGHT" equal 0, 1 and 2. how can i pass the parameter so that i can get all values.

kindly give suggestion for my problem.

Thanks in advance.

2
  • I've had the same problem before, and after doing a fair bit of research, could not find a way to do it. Commented Oct 25, 2013 at 6:49
  • @Liang lets see there is any solution from others, then what u did for this problem or which way, i means what alternative way u used for solve this problem Commented Oct 25, 2013 at 6:52

1 Answer 1

2

Change your enum to:

{
MOVE_NONE = 0
MOVE_UP = 1<<0,
MOVE_DOWN = 1<<1,
MOVE_RIGHT = 1<<2,
MOVE_LEFT = 1<<3,
};

So you can pass parameters exactly as you want: MOVE_UP|MOVE_DOWN|MOVE_RIGHT.

And in invokeEvents:withMessage: check

if(events & MOVE_UP)
{
}
if(events & MOVE_DOWN)
{
}
...

.

Sign up to request clarification or add additional context in comments.

2 Comments

how can i store this value in array. for example: events = MOVE_UP|MOVE_DOWN means value of the events is MOVE_UP = 0001 and MOVE_DOWN = 0010. just assume only 4 bits now the value is 0011 i want to store this value. which data type is fit for this in objective-c.
It's NSInteger that may be wrapped NSNumber object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.