开发者

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

开发者 https://www.devze.com 2023-02-09 04:22 出处:网络
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:

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)];

Example:

[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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消