How to create method with parameter like: [object call:(TypeOne | TypeTwo | TypeThree)];

I'm trying to write a method in objc with a parameter that takes optional numer of types. Like the autorezise property for UIView. Or this one:

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];


[object call:(TypeOne | TypeTwo | TypeThree)];

My guess is to define a method that takes a enum type like this:

- (void)call:(EnumType)type;

But then i have no idea how to act on the "type". Can i use an if statement?

-------------Problems Reply------------

it's declared as

typedef enum {
UIRemoteNotificationTypeNone = 0,
UIRemoteNotificationTypeBadge = 1 << 0,
UIRemoteNotificationTypeSound = 1 << 1,
UIRemoteNotificationTypeAlert = 1 << 2
} UIRemoteNotificationType;

there is an associated type. therefore, yes - (void)call:(EnumType)type; is correct.

to act on it: enum types behave like an int in many ways. you can if, compare, switch, and so on.

To test a value of a bitfield suggested by Justin, use & operator:

if ( type & TypeOne )
// TypeOne bit is set

For this to work TypeOne, TypeTwo, etc. must be integers which have exactly one bit set to 1 with the remaining bits set to 0.

You can write a switch statement. And remember that enumeration of the identifiers start from 0 and increment by 1 by default. Write case statements based up on your enumeration identifier's value.

Category:iphone Views:1 Time:2011-02-07

Related post

Copyright (C), All Rights Reserved.

processed in 0.208 (s). 11 q(s)