NS_ENUM与NS_OPTIONS的区别

NS_ENUM

说明:它用于定义一般的NSInteger类型的枚举,在使用其枚举值时,每次只能选择其中一项,如:

  1. typedef NS_ENUM(NSInteger, UIViewContentMode) {
  2. UIViewContentModeScaleToFill,
  3. UIViewContentModeScaleAspectFit,
  4. UIViewContentModeScaleAspectFill,
  5. UIViewContentModeRedraw,
  6. UIViewContentModeCenter,
  7. UIViewContentModeTop,
  8. UIViewContentModeBottom,
  9. UIViewContentModeLeft,
  10. UIViewContentModeRight,
  11. UIViewContentModeTopLeft,
  12. UIViewContentModeTopRight,
  13. UIViewContentModeBottomLeft,
  14. UIViewContentModeBottomRight,
  15. };
  16. NSInteger contentMode = UIViewContentModeScaleAspectFit; // 只能指定一个枚举值,不能同时指定多个

注意:通过NS_ENUM定义枚举时,如果没有手动给每个枚举值赋值,那么会给每个枚举值赋予从0开始依次累加1的值。




NS_OPTIONS

说明:它用于定义位掩码(bitmasked)类型的枚举,其枚举值可以进行组合使用,如:

  1. typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
  2. UIViewAutoresizingNone = 0,
  3. UIViewAutoresizingFlexibleLeftMargin = 1 << 0,
  4. UIViewAutoresizingFlexibleWidth = 1 << 1,
  5. UIViewAutoresizingFlexibleRightMargin = 1 << 2,
  6. UIViewAutoresizingFlexibleTopMargin = 1 << 3,
  7. UIViewAutoresizingFlexibleHeight = 1 << 4,
  8. UIViewAutoresizingFlexibleBottomMargin = 1 << 5
  9. };
  10. NSUInteger autoresizing = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; // 可以同时指定多个枚举值,进行组合使用




系统如何知道我们是否使用了多个枚举值组合?

  1. 首先,将各枚举值组合之后的新值(如:NSUInteger autoresizing = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;,即按位或运算),分别与各个枚举值进行按位与运算(如:NSUInteger value1 = autoresizing & UIViewAutoresizingFlexibleWidth);

  2. 然后,判断按位与运算的结果是否与目标枚举值相等(如:value1 == UIViewAutoresizingFlexibleWidth),若相等,则说明包含该枚举值,否则说明不包含。

更详细地说明可以阅读这篇文章