objective-c - ios , 关于多 参数 枚举 的实现?
本文介绍了objective-c - ios , 关于多 参数 枚举 的实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
我想实现 可以 传入 多参数枚举值的方法,例如
,请教一下,方法里面的逻辑判断
解决方案
你代码里展示的 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。
}
这篇关于objective-c - ios , 关于多 参数 枚举 的实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文