美文网首页
Tips:NS_OPTIONS

Tips:NS_OPTIONS

作者: dc630f46ee2d | 来源:发表于2017-10-25 00:56 被阅读0次

    前言

    在阅读sdwebimageView的时候,有一个NS_OPTIONS类的枚举类型

    typedef NS_OPTIONS(NSUInteger, SDWebImageOptions) {
        /**
         * By default, when a URL fail to be downloaded, the URL is blacklisted so the library won't keep trying.
         * This flag disable this blacklisting.
         */
        SDWebImageRetryFailed = 1 << 0,
    
        /**
         * By default, image downloads are started during UI interactions, this flags disable this feature,
         * leading to delayed download on UIScrollView deceleration for instance.
         */
        SDWebImageLowPriority = 1 << 1,
    
        /**
         * This flag disables on-disk caching
         */
        SDWebImageCacheMemoryOnly = 1 << 2,
    
        /**
         * This flag enables progressive download, the image is displayed progressively during download as a browser would do.
         * By default, the image is only displayed once completely downloaded.
         */
        SDWebImageProgressiveDownload = 1 << 3,
    
        /**
         * Even if the image is cached, respect the HTTP response cache control, and refresh the image from remote location if needed.
         * The disk caching will be handled by NSURLCache instead of SDWebImage leading to slight performance degradation.
         * This option helps deal with images changing behind the same request URL, e.g. Facebook graph api profile pics.
         * If a cached image is refreshed, the completion block is called once with the cached image and again with the final image.
         *
         * Use this flag only if you can't make your URLs static with embedded cache busting parameter.
         */
        SDWebImageRefreshCached = 1 << 4,
    
        /**
         * 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.
         */
        SDWebImageContinueInBackground = 1 << 5,
    
        /**
         * Handles cookies stored in NSHTTPCookieStore by setting
         * NSMutableURLRequest.HTTPShouldHandleCookies = YES;
         */
        SDWebImageHandleCookies = 1 << 6,
    
        /**
         * Enable to allow untrusted SSL certificates.
         * Useful for testing purposes. Use with caution in production.
         */
        SDWebImageAllowInvalidSSLCertificates = 1 << 7,
    
        /**
         * By default, images are loaded in the order in which they were queued. This flag moves them to
         * the front of the queue.
         */
        SDWebImageHighPriority = 1 << 8,
        
        /**
         * By default, placeholder images are loaded while the image is loading. This flag will delay the loading
         * of the placeholder image until after the image has finished loading.
         */
        SDWebImageDelayPlaceholder = 1 << 9,
    
        /**
         * We usually don't call transformDownloadedImage delegate method on animated images,
         * as most transformation code would mangle it.
         * Use this flag to transform them anyway.
         */
        SDWebImageTransformAnimatedImage = 1 << 10,
        
        /**
         * By default, image is added to the imageView after download. But in some cases, we want to
         * have the hand before setting the image (apply a filter or add it with cross-fade animation for instance)
         * Use this flag if you want to manually set the image in the completion when success
         */
        SDWebImageAvoidAutoSetImage = 1 << 11,
        
        /**
         * By default, images are decoded respecting their original size. On iOS, this flag will scale down the
         * images to a size compatible with the constrained memory of devices.
         * If `SDWebImageProgressiveDownload` flag is set the scale down is deactivated.
         */
        SDWebImageScaleDownLargeImages = 1 << 12
    };
    

    这里的枚举值的可以多种状态同时存在,即可以只内存缓存,同时关闭失败重试,打开后台下载等各种功能。
    不同于NS_ENUM类型的枚举变量,只能保存一个状态
    NS_OPTIONS类型实现来一个枚举变量,保存多种状态的目的。

    基本格式

    typedef NS_OPTIONS(NSUInteger, MyEnum) {
        MyEnumValueA = 1 << 0, // 00000001
        MyEnumValueB = 1 << 1, // 00000010
        MyEnumValueC = 1 << 2,//  00000100
    };
    

    同时包含可以用 |

    MyEnumValueA | MyEnumValueB 同时存在A和B两种状态,而且任意几个的状态的|不会等于其它的枚举值

    添加一个状态用|=

    MyEnum  value= MyEnumValueA |  MyEnumValueB;
    value |= MyEnumValueC; // 添加MyEnumValueC状态
    

    判断是否包含&

    if(value & MyEnumValueC){
          NSLog(@"包含MyEnumValueC");
    }
    // 位与如果value和MyEnumValueC在同一个位为1,那么整个结果肯定不为0,为True
    

    DEMO

    一个人可以有多种国籍,用Country枚举变量保存

    - (void)viewDidLoad {
        [super viewDidLoad];
        Country country = China | Japan; // 位运算,表示同时存在,这个是有双重国籍的人
        country |= Korea; // 添加一个Korea, country = country | Korea;
        NSLog(@"%ld", country);
        if(country & Japan){  // country是否包含Japan
            NSLog(@"%ld", country);
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:Tips:NS_OPTIONS

          本文链接:https://www.haomeiwen.com/subject/niqnuxtx.html