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");
}
}