Objective-C 中的类型推导 __auto_type

最近在看谷歌开源的Promises,发现他内部对block的定义居然用了__auto_type,就查了下,原来它就是oc内部实现的像swift一样的 let 和 var,可以对 对象进行类型推导,方便很多
eg:

    __auto_type string = @"test";
    __auto_type subString = [string substringFromIndex:1];
    NSLog(@"%@",subString);
//正常写法
    void(^testBlock)(NSString *,NSNumber *) = ^(NSString *string,NSNumber *number){
        NSLog(@"testBlock %@ - %@",string,number);
    };
//类型推导
    __auto_type test = ^(NSString *string,NSNumber *number){
        NSLog(@"__auto_type %@ - %@",string,number);
    };
    testBlock(@"1",@(2));
    test(@"3",@(4));

你可能感兴趣的:(Objective-C 中的类型推导 __auto_type)