typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
UIViewAutoresizingNone = 0,
UIViewAutoresizingFlexibleLeftMargin = 1 << 0,
UIViewAutoresizingFlexibleWidth = 1 << 1,
UIViewAutoresizingFlexibleRightMargin = 1 << 2,
UIViewAutoresizingFlexibleTopMargin = 1 << 3,
UIViewAutoresizingFlexibleHeight = 1 << 4,
UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
复制代码
UIViewAutoresizing autoresizing = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
复制代码
typedef NS_ENUM(NSInteger, LTSeason) {
LTSeasonSpring = 1 << 0, // 0b0001
LTSeasonSummer = 1 << 1, // 0b0010
LTSeasonAutumn = 1 << 2, // 0b0100
LTSeasonWinter = 1 << 3 // 0b1000
};
复制代码
LTSeason seasons = LTSeasonSpring | LTSeasonSummer | LTSeasonWinter;
复制代码
- 这三个枚举值经过逻辑或运算后,
seasons
的结果如下
0001
0010
|1000
------
1011
即: seasons = 0b1011
复制代码
- 通过
逻辑或
得到的seasons
, 我们在使用时, 需要将其拆开, 看看是由哪些枚举值组成的 - 以
LTSeasonWinter
为例, 可以使用下面的方式获取是否含有LTSeasonWinter
1011
&1000
------
1000
获取到的结果大于0, 说明含有LTSeasonWinter
复制代码
- 而
LTSeasonWinter
的值正好是1000
- 所以我们可以通过下面的方式判断,
seasons
中是否含有指定枚举
- (void)setSeasons:(LTSeason)seasons
{
if (seasons & LTSeasonSpring) {
NSLog(@"seasons 包含了 LTSeasonSpring");
}
if (seasons & LTSeasonSummer) {
NSLog(@"seasons 包含了 LTSeasonSummer");
}
if (seasons & LTSeasonAutumn) {
NSLog(@"seasons 包含了 LTSeasonAutumn");
}
if (seasons & LTSeasonWinter) {
NSLog(@"seasons 包含了 LTSeasonWinter");
}
}
复制代码