美文网首页ios
OC中小笔录

OC中小笔录

作者: CoderHG | 来源:发表于2016-01-02 21:56 被阅读259次

一、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];
        });
    });
}

谢谢!

相关文章

  • OC中小笔录

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

  • 数字Number

    OC中小数的精确计算http://www.tuicool.com/articles/QzMFZf kCFNumbe...

  • OC中小数的精确计算

    问题源于自己做的一个字符串单位换算的扩展方法,目的是把一大串数字转换成万、百万等为单位。最开始的时候,思路很简单 ...

  • 开始朗读《文学回忆录》

    作者木心、笔录陈丹青

  • 怎样制作搜查笔录才有证据能力

    导语 搜查笔录在“持有”、“窝藏”、“妨害司法”类犯罪中具有其他证据不可替代的作用,但是搜查笔录要具有证据能力,其...

  • angular 1x -- Ui-Router Dome

    ui-router 笔录 HTML JS 重定向

  • 笔录

    们不愿受到我们最好的敌手照顾,也不愿受到我们衷心喜爱的人照顾

  • 笔录

    我是一个刚毕业的准大学生,以下是我高考前一个月有试卷背而留下的笔录! ﹌﹌﹌﹌﹌分割线﹌﹌﹌﹌ 那天听毛不易的歌。...

  • 笔录

    1. 每个人想要真正强大起来 都要度过一段没人帮忙 没人支持的日子 所有事情都是自己一个人撑 所有情绪都是只有自己...

  • 笔录

    题记:步伐每往前走一步,回头便是历史。 04 因为优秀的人比你更努力,所以比你获得的更多这件事是必然的。 笔者工作...

网友评论

    本文标题:OC中小笔录

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