search

Home  >  Q&A  >  body text

objective-c - ios , 关于多 参数 枚举 的实现?

我想实现 可以 传入 多参数枚举值的方法,例如

,请教一下,方法里面的逻辑判断

漂亮男人漂亮男人2754 days ago722

reply all(1)I'll reply

  • 阿神

    阿神2017-05-02 09:21:11

    The UIRectCornerTopLeft and UIRectCornerTopRight shown in your code are actually not enumerations, but 按位掩码(bitmask), and their definition is as follows:

    typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
        UIRectCornerTopLeft     = 1 << 0,
        UIRectCornerTopRight    = 1 << 1,
        UIRectCornerBottomLeft  = 1 << 2,
        UIRectCornerBottomRight = 1 << 3,
        UIRectCornerAllCorners  = ~0UL
    };

    The syntax of a bitwise mask (NS_OPTIONS) is the same as an enumeration (NS_ENUM), but the compiler will combine its values ​​| through a bitmask.

    Editor:

    For example, for the NS_OPTIONS of UIRectCorner above, according to your code, what you pass in is UIRectCornerTopLeft | UIRectCornerTopRight , then the processing code is roughly as follows:

    UIRectCorner myRectCornerOptions = UIRectCornerTopLeft | UIRectCornerTopRight; // 你在方法里接收到值应该是这个。
    
    // 对传入的 NS_OPTIONS 的处理逻辑:
    if (myRectCornerOptions & UIRectCornerTopLeft) {
        // 包含了 UIRectCornerTopLeft。
    } else {
        // 未包含 UIRectCornerTopLeft。
    }
        
    if (myRectCornerOptions & UIRectCornerTopRight) {
        // 包含了 UIRectCornerTopRight。
    } else {
        // 未包含 UIRectCornerTopRight。
    }

    reply
    0
  • Cancelreply