我正在尝试实现以下typedef
typedef NS_OPTIONS (NSInteger, MyCellCorners) {
MyCellCornerTopLeft,
MyCellCornerTopRight,
MyCellCornerBottomLeft,
MyCellCornerBottomRight,
};
Run Code Online (Sandbox Code Playgroud)
并正确分配值
MyCellCorners cellCorners = (MyCellCornerTopLeft | MyCellCornerTopRight);
Run Code Online (Sandbox Code Playgroud)
在绘制我的单元格时,如何检查哪些选项匹配,以便我可以正确绘制它.
小智 52
使用位掩码:
typedef NS_OPTIONS (NSInteger, MyCellCorners) {
MyCellCornerTopLeft = 1 << 0,
MyCellCornerTopRight = 1 << 1,
MyCellCornerBottomLeft = 1 << 2,
MyCellCornerBottomRight = 1 << 3,
};
MyCellCorners cellCorners = MyCellCornerTopLeft | MyCellCornerTopRight;
if (cellCorners & MyCellCornerTopLeft) {
// top left corner set
}
if (etc...) {
}
Run Code Online (Sandbox Code Playgroud)
小智 21
检查此值的正确方法是首先按位AND值,然后检查是否与所需值相等.
MyCellCorners cellCorners = MyCellCornerTopLeft | MyCellCornerTopRight;
if ((cellCorners & MyCellCornerTopLeft) == MyCellCornerTopLeft) {
// top left corner set
}
Run Code Online (Sandbox Code Playgroud)
以下参考资料解释了为什么这是正确的,并提供了枚举类型的其他见解.
参考:检查位值掩码中的值
| 归档时间: |
|
| 查看次数: |
9077 次 |
| 最近记录: |