简体   繁体   中英

enum values passing as arguments in Objective-C

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.

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)
{
}
...

.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM