iOS开发中的小知识点

2019-08-16  本文已影响0人  Frankkkkk

一、weak和assign的区别

相同点:

都不会强引用。

不同点:

二、位移的理解:

2.1 SDWebImage枚举中的位移

#import "SDWebImageCompat.h"
#import "SDWebImageOperation.h"
#import "SDWebImageDownloader.h"
#import "SDImageCache.h"

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 after the download finished, only cache in memory
     */
    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,

    ......

};

1 << 0表示数字1往左边移动0位(即位置不动)
1 << 1表示数字1往左边移动1位,即从0001变成0010,为2
1 << 2表示数字1往左边移动2位,即从0001变成0100,为4
以此类推。

2.2 位移的作用:| 按位或, &按位与
示例代码:

#import "ViewController.h"

// C 语言的语法
//typedef enum {
//    ActionTypeTop       = 1 << 0,
//    ActionTypeBottom    = 1 << 1,
//    ActionTypeLeft      = 1 << 2,
//    ActionTypeRight     = 1 << 3
//} ActionType;

// OC 在 iOS 5.0 推出的,可以指定枚举的数据类型
// NS_OPTIONS,就可以用 | & 进行操作
// typedef NS_ENUM 定义非位移的枚举
typedef NS_OPTIONS(NSUInteger, ActionType) {
    ActionTypeTop       = 1 << 0,
    ActionTypeBottom    = 1 << 1,
    ActionTypeLeft      = 1 << 2,
    ActionTypeRight     = 1 << 3
};

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    // `|` 按位或
    // 能够组合传递参数,而且只用一个整数就能够搞定!
    // 约定:如果发现枚举类型是位移的,传入 0,表示什么附加操作都不做,效率最高!
    ActionType type = ActionTypeTop | ActionTypeBottom | ActionTypeLeft | ActionTypeRight;
    [self action:0];
}

// 需求:传入不同的 type,能够做出不同的响应
// 但是:type 能够各种组合
- (void)action:(ActionType)type {
    
    if (type == 0) {
        NSLog(@"没有类型");
        return;
    }
    
    NSLog(@"%tu", type);
    
    // `&` 按位与
    if ((type & ActionTypeTop) == ActionTypeTop) {
        NSLog(@"top %tu", (type & ActionTypeTop));
    }
    if ((type & ActionTypeBottom) == ActionTypeBottom) {
        NSLog(@"bottom %tu", (type & ActionTypeBottom));
    }
    if ((type & ActionTypeLeft) == ActionTypeLeft) {
        NSLog(@"left %tu", (type & ActionTypeLeft));
    }
    if ((type & ActionTypeRight) == ActionTypeRight) {
        NSLog(@"right %tu", (type & ActionTypeRight));
    }
}

@end
上一篇下一篇

猜你喜欢

热点阅读