iOS-位移枚举简单介绍
生活随笔
收集整理的這篇文章主要介紹了
iOS-位移枚举简单介绍
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
路漫漫其修遠兮,吾將上下而求索。
在蘋果系統以及我們常用的著名的三方框架中,我們經常會遇到位運算,比如像系統中的UIView動畫里面的位運算<<
亦或像下面的SDWebImage里面的位運算:
typedef NS_OPTIONS(NSUInteger, SDWebImageDownloaderOptions) {SDWebImageDownloaderLowPriority = 1 << 0,SDWebImageDownloaderProgressiveDownload = 1 << 1,/*** By default, request prevent the use of NSURLCache. With this flag, NSURLCache* is used with default policies.*/SDWebImageDownloaderUseNSURLCache = 1 << 2,/*** Call completion block with nil image/imageData if the image was read from NSURLCache* (to be combined with `SDWebImageDownloaderUseNSURLCache`).*/SDWebImageDownloaderIgnoreCachedResponse = 1 << 3,/*** In iOS 4+, continue the download of the image if the app goes to background. This is achieved by asking the system for* extra time in background to let the request finish. If the background task expires the operation will be cancelled.*/SDWebImageDownloaderContinueInBackground = 1 << 4,/*** Handles cookies stored in NSHTTPCookieStore by setting * NSMutableURLRequest.HTTPShouldHandleCookies = YES;*/SDWebImageDownloaderHandleCookies = 1 << 5,/*** Enable to allow untrusted SSL certificates.* Useful for testing purposes. Use with caution in production.*/SDWebImageDownloaderAllowInvalidSSLCertificates = 1 << 6,/*** Put the image in the high priority queue.*/SDWebImageDownloaderHighPriority = 1 << 7,/*** Scale down the image*/SDWebImageDownloaderScaleDownLargeImages = 1 << 8, };那么這種枚舉有什么不一樣的地方呢,其實這種枚舉類型非常強大,也非常好用,下面通過一個小例子來說明:
// 第一種寫法 -這是C的寫法 typedef enum {EnumDemoTypeTop,EnumDemoType1Bottom,}EnumDemoType1;//第二種寫法 -這是OC的寫法 typedef NS_ENUM(NSInteger, EnumDemoType2) {EnumDemoTypeLeft,EnumDemoTypeRight, };//第三種寫法 typedef NS_OPTIONS(NSInteger, EnumDemoActionType) {EnumDemoActionTypeTop = 1<<0, //1*2(0) = 1 /1*二的零次方 = 1EnumDemoActionType1Bottom = 1<<1, //1*2(1)=2EnumDemoActionTypeLeft = 1<<2,//1*2(2)=4EnumDemoActionTypeRight = 1<<3,//1*2(3)=8 };使用位運算,就可以同時支持多個值:
- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib. }- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {[self enumDemoType:EnumDemoActionTypeTop | EnumDemoActionType1Bottom | EnumDemoActionTypeLeft | EnumDemoActionTypeRight]; }//按位與 & 1&1==1 1&0==0 0&0==1 只要有0則為0 //按位或 | 1|1==1 1|0==1 0|0=0 只要有1則為1 - (void)enumDemoType:(EnumDemoActionType)type {NSLog(@"type:%ld",type);if (type & EnumDemoActionTypeTop) {NSLog(@"向上----%zd",type & EnumDemoActionTypeTop);}if (type & EnumDemoActionType1Bottom) {NSLog(@"向下----%zd",type & EnumDemoActionType1Bottom);}if (type & EnumDemoActionTypeLeft) {NSLog(@"向左----%zd",type & EnumDemoActionTypeLeft);}if (type & EnumDemoActionTypeRight) {NSLog(@"向右----%zd",type & EnumDemoActionTypeRight);} }充分的用好枚舉,可以增強代碼的可讀性與健壯性,讓代碼更加的規范。
總結
以上是生活随笔為你收集整理的iOS-位移枚举简单介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php对英语单词,php英语单词,php
- 下一篇: php arrayiteratoer,手