问题描述
你知道在 Cocoa 中有这个东西,例如你可以创建一个 UIView
并执行:
You know in Cocoa there is this thing, for example you can create a UIView
and do:
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
我有一个具有多个状态的自定义 UIView
,我在 enum
中定义了它,如下所示:
I have a custom UIView
with multiple states, which I have defined in an enum
like this:
enum DownloadViewStatus {
FileNotDownloaded,
FileDownloading,
FileDownloaded
};
对于每个创建的子视图,我设置它的tag
:subview1.tag = FileNotDownloaded;
For each created subview, I set its tag
: subview1.tag = FileNotDownloaded;
然后,我有一个自定义的视图状态设置器,它执行以下操作:
Then, I have a custom setter for the view state which does the following:
for (UIView *subview in self.subviews) {
if (subview.tag == viewStatus)
subview.hidden = NO;
else
subview.hidden = YES;
}
但是我正在尝试做的,是允许这样做:
subview1.tag = FileNotDownloaded | FileDownloaded;
所以我的 subview1
出现在我视图的两种状态中.目前,它不会出现在这两种状态中的任何一种,因为 |
运算符似乎将两个枚举值相加.
So my subview1
shows up in two states of my view. Currently, it doesn't show up in any of those two states since the |
operator seems to add the two enum values.
有没有办法做到这一点?
Is there a way to do that?
推荐答案
声明位掩码:
除了分配绝对值(1
、2
、4
、...),您还可以声明 位掩码(这些是如何命名的)像这样:
Declaring Bitmasks:
Alternatively to assigning absolute values (1
, 2
, 4
, …) you can declare bitmasks (how these are called) like this:
typedef enum : NSUInteger {
FileNotDownloaded = (1 << 0), // => 00000001
FileDownloading = (1 << 1), // => 00000010
FileDownloaded = (1 << 2) // => 00000100
} DownloadViewStatus;
或使用现代 ObjC 的 NS_OPTIONS
/NS_ENUM
宏:
or using modern ObjC's NS_OPTIONS
/NS_ENUM
macros:
typedef NS_OPTIONS(NSUInteger, DownloadViewStatus) {
FileNotDownloaded = (1 << 0), // => 00000001
FileDownloading = (1 << 1), // => 00000010
FileDownloaded = (1 << 2) // => 00000100
};
(有关后者的更多信息,请参阅 Abilern 的回答)
(see Abizern's answer for more info on the latter)
位掩码的概念是(通常)用单个位集定义每个枚举值.
The concept of bitmasks is to (usually) define each enum value with a single bit set.
因此 OR
对两个值执行以下操作:
Hence OR
ing two values does the following:
DownloadViewStatus status = FileNotDownloaded | FileDownloaded; // => 00000101
相当于:
00000001 // FileNotDownloaded
| 00000100 // FileDownloaded
----------
= 00000101 // (FileNotDownloaded | FileDownloaded)
比较位掩码:
检查位掩码时要记住的一件事:
Comparing Bitmasks:
One thing to keep in mind when checking against bitmasks:
假设状态初始化如下:
DownloadViewStatus status = FileNotDownloaded | FileDownloaded; // => 00000101
如果要检查 status
equals FileNotDownloaded
是否可以使用:
If you want to check if status
equals FileNotDownloaded
, you can use:
BOOL equals = (status == FileNotDownloaded); // => false
相当于:
00000101 // (FileNotDownloaded | FileDownloaded)
== 00000100 // FileDownloaded
-----------
= 00000000 // false
检查会员":
如果你想检查status
是否仅仅contains FileNotDownloaded
,你需要使用:
Checking for "membership":
If you want to check if status
merely contains FileNotDownloaded
, you need to use:
BOOL contains = (status & FileNotDownloaded) != 0; // => true
00000101 // (FileNotDownloaded | FileDownloaded)
& 00000100 // FileDownloaded
-----------
= 00000100 // FileDownloaded
!= 00000000 // 0
-----------
= 00000001 // 1 => true
看到细微的差别(以及为什么您当前的if"表达式可能是错误的)?
See the subtle difference (and why your current "if"-expression is probably wrong)?
这篇关于在 Objective-C 中声明和检查/比较(位掩码-)枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!