枚舉在項目中使用的比較多,目的是規範業務。做用主要分states和options。微信
states是單選,表示當前處於某鍾狀態。例如:應用當前網絡是4G。
options能夠使多選,表示處於多種狀況下,例如:應用支持支付寶和微信支付。網絡
typedef NS_ENUM(NSUInteger, XX_NetworkTrafficType) { XX_NetworkTrafficTypeWWAN_3G = 0, XX_NetworkTrafficTypeWWAN_4G, XX_NetworkTrafficTypeWIFI, //... };
若是位運算須要學習的話,能夠看這裏 - matrix67.位運算學習
typedef NS_OPTIONS(NSUInteger, XX_ThirdPayType) { XX_ThirdPayTypeWechat = 1 << 0, //*> 微信支付 XX_ThirdPayTypeAliPay = 1 << 1, //*> 支付寶支付 XX_ThirdPayTypeC_UnionPay = 1 << 2, //*> 銀聯支付 XX_ThirdPayTypeApplePay = 1 << 3 //*> 蘋果支付 XX_ThirdPayTypePayPal = 1 << 4, //*> Paypal支付 }; XX_ThirdPayType myPayType = XX_ThirdPayTypeAliPay | XX_ThirdPayTypeWechat | XX_ThirdPayTypeApplePay; NSLog(@"%lu",(unsigned long)myPayType); // 打印:11
爲何option使用位運算呢?舉例:myPayType = 11; 正是利用"|"或運算的組合拳。
因此咱們還能夠這麼聲明Options微信支付
typedef NS_OPTIONS(NSUInteger, XX_ThirdPayType) { XX_ThirdPayTypeWechat = 1 << 0, //*> 微信支付 XX_ThirdPayTypeAliPay = 1 << 1, //*> 支付寶支付 XX_ThirdPayTypeC_UnionPay = 1 << 2, //*> 銀聯支付 XX_ThirdPayTypeApplePay = 1 << 3, //*> 蘋果支付 XX_ThirdPayTypePayPal = 1 << 4, //*> Paypal支付 XX_ThirdPayTypeChinaPay = XX_ThirdPayTypeWechat | XX_ThirdPayTypeAliPay | XX_ThirdPayTypeC_UnionPay, //*> 中國國內支付平臺 XX_ThirdPayTypeForeignPay = XX_ThirdPayTypeApplePay | XX_ThirdPayTypePayPal, //*> 中國國外支付平臺 XX_ThirdPayTypeAllPay = XX_ThirdPayTypeChinaPay | XX_ThirdPayTypeForeignPay //*> 全部支付平臺 };
說明:
NS_ENUM 和 NS_OPTIONS 分別是 Foundation 提供給Objective-C 定義狀態和選擇的枚舉定義方法。code