问题描述
我想实现 可以 传入 多参数枚举值的方法,例如,请教一下,方法里面的逻辑判断
问题解答
回答1:你代码里展示的 UIRectCornerTopLeft、UIRectCornerTopRight 其实并不是枚举,而是按位掩码(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 ,那么处理时候的代码大致如下:
UIRectCorner myRectCornerOptions = UIRectCornerTopLeft | UIRectCornerTopRight; // 你在方法里接收到值应该是这个。// 对传入的 NS_OPTIONS 的处理逻辑:if (myRectCornerOptions & UIRectCornerTopLeft) { // 包含了 UIRectCornerTopLeft。} else { // 未包含 UIRectCornerTopLeft。} if (myRectCornerOptions & UIRectCornerTopRight) { // 包含了 UIRectCornerTopRight。} else { // 未包含 UIRectCornerTopRight。}