我想實現 可以 傳入 多參數枚舉值的方法,例如,請教一下,方法裡面的邏輯判斷
你程式碼裡展示的 UIRectCornerTopLeft、UIRectCornerTopRight 其實不是枚舉,而是按位掩码(bitmask),它的定義如下:
按位掩码(bitmask)
typedef NS_OPTIONS(NSUInteger, UIRectCorner) { UIRectCornerTopLeft = 1 << 0, UIRectCornerTopRight = 1 << 1, UIRectCornerBottomLeft = 1 << 2, UIRectCornerBottomRight = 1 << 3, UIRectCornerAllCorners = ~0UL };
按位遮罩(NS_OPTIONS)的語法和枚舉(NS_ENUM)相同,但編譯器會將它的值透過位元遮罩 | 組合在一起。
|
例如對於上面的 UIRectCorner 這個 NS_OPTIONS,按照你的代碼,你傳入的是 UIRectCornerTopLeft | UIRectCornerTopRight ,那麼處理時候的代碼大致如下:
UIRectCornerTopLeft | UIRectCornerTopRight
UIRectCorner myRectCornerOptions = UIRectCornerTopLeft | UIRectCornerTopRight; // 你在方法里接收到值应该是这个。 // 对传入的 NS_OPTIONS 的处理逻辑: if (myRectCornerOptions & UIRectCornerTopLeft) { // 包含了 UIRectCornerTopLeft。 } else { // 未包含 UIRectCornerTopLeft。 } if (myRectCornerOptions & UIRectCornerTopRight) { // 包含了 UIRectCornerTopRight。 } else { // 未包含 UIRectCornerTopRight。 }
你程式碼裡展示的 UIRectCornerTopLeft、UIRectCornerTopRight 其實不是枚舉,而是
按位掩码(bitmask)
,它的定義如下:按位遮罩(NS_OPTIONS)的語法和枚舉(NS_ENUM)相同,但編譯器會將它的值透過位元遮罩
|
組合在一起。編輯:
例如對於上面的 UIRectCorner 這個 NS_OPTIONS,按照你的代碼,你傳入的是
UIRectCornerTopLeft | UIRectCornerTopRight
,那麼處理時候的代碼大致如下: