OC中小笔录

一、instancetype与id

在很多时候,总是会看到instancetype与id这两个关键字.似乎会感觉是同一样东西.

存在就是真理!

接下来,就让我带领大家研究一下这两个关键字的异同:

直接看代码:

.h文件
#import <Foundation/Foundation.h>

@interface HGSquareMode : NSObject

#pragma mark - 属性
@property (nonatomic, copy) NSString* title;
@property (nonatomic, copy) NSString* subTitle;

// 实例方法
- (id)initWithDict:(NSDictionary*)dict;

// 类方法
+ (id)squareWithDict:(NSDictionary*)dict;

@end

.m文件
#import "HGSquareMode.h"

@implementation HGSquareMode

- (id)initWithDict:(NSDictionary*)dict {
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}

+ (id)squareWithDict:(NSDictionary*)dict {
    return [[self alloc] initWithDict:dict];
}

@end

上面有分别有两个方法:1.实例方法(initWithDict:),2.类方法(squareWithDict:)
返回值,都为id类型.
回到控制器(HGinstancetypeViewController)中,代码如下:

#import "HGinstancetypeViewController.h"
#import "HGSquareMode.h"

@interface HGinstancetypeViewController ()

@end

@implementation HGinstancetypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSDictionary* instancetypeDict = @{
                                       @"title":@"instancetype试演",
                                       @"subTitle":@"HGinstancetypeViewController"
                                       };
    NSArray* instancetypeArr = [HGSquareMode squareWithDict:instancetypeDict];
    
    HGLog(@"%zd", instancetypeArr.count);
}

@end
会闪退

上面的代码你build时没有问题,能通过。但是如果是运行呢?所以上面的代码是有危险的。

再看一下下面的代码(仅仅是改了HGSquareMode两个方法的返回值):

.h文件
#import <Foundation/Foundation.h>

@interface HGSquareMode : NSObject

#pragma mark - 属性
@property (nonatomic, copy) NSString* title;
@property (nonatomic, copy) NSString* subTitle;

// 实例方法
- (instancetype)initWithDict:(NSDictionary*)dict;

// 类方法
+ (instancetype)squareWithDict:(NSDictionary*)dict;

@end


.m文件
#import "HGSquareMode.h"

@implementation HGSquareMode

- (instancetype)initWithDict:(NSDictionary*)dict {
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}

+ (instancetype)squareWithDict:(NSDictionary*)dict {
    return [[self alloc] initWithDict:dict];
}

@end

然后我在控制器(HGinstancetypeViewController)中截图看一下:

有警告⚠
有警告!对,这就是区别!

总结instancetype与id的异同

  • instancetype,编译器能正确的推断出返回实例的实际类型!
  • instancetype只能用于返回值!
    建议在返回self实例的方法中,用instancetype,别用id.

二、线程欣赏

名词解释

很多人对下面的4个名词,或多或少的在理解上会有偏差:

  • 同步: 不具备开启线程的能力(dispatch_sync)
  • 异步: 具备开启线程的能力(dispatch_async)
  • 并列队列: 多个任务可以同时执行
  • 串行队列: 一个任务执行完后,再执行下一个任务

常用的的宏定义

#define CoderHGGlobalQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
#define CoderHGMainQueue dispatch_get_main_queue()

金典的用例

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    dispatch_async(CoderHGGlobalQueue, ^{
        // 1.子线程
        NSString* urlStr = @"";
        NSURL *url = [NSURL URLWithString:urlStr];
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *image = [UIImage imageWithData:data];

        // 2.回主线程设置图片
        dispatch_async(CoderHGMainQueue, ^{
            [self.imageView setImage:image];
        });
    });
}

谢谢!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,218评论 19 139
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,707评论 0 17
  • 2014年7月中旬参加了一次保险新人班培训,本来是抱着去玩玩的态度去的,没想到给我带来了很大的冲击,很多的思考,直...
    王丽燕199阅读 571评论 0 2
  • 做为一名产品小白,并且还有在转型路上的小白,在自己能力以及知识没有一定的沉垫前,可能还不应孩去谈我所在行业的产...
    澎大力阅读 578评论 1 49
  • 今天内心“暴动”了一天,情绪时不时的会如火山般喷发。 01 不知道是因为受师傅现在同事的影响,还是因为外面的客户真...
    爱学爱践行的十四阅读 262评论 0 3