Objective-C: Check if using enum option

后端 未结 4 941
醉话见心
醉话见心 2021-02-06 13:51

I have a custom object that is using a typedef enum. If I set a few of the enum options for my object, how can I check to see if those are being used?

typedef e         


        
4条回答
  •  情话喂你
    2021-02-06 14:40

    I'd suggest to define the enum using NS_OPTIONS. This is the Apple recommended way to create such enums.

    typedef NS_OPTIONS(NSUInteger, Options) {
        Options1 = 1 << 0,
        Options2 = 1 << 1,
        Options3 = 1 << 2,
    };
    

    Then, as it has already been said, you can assign values by doing:

    myObject.options = Option1 | Option2;
    

    and check them:

    if (myObject.options & Option1) {
        // Do something
    }
    

提交回复
热议问题