id instancetype __kindof 都是用于方法的返回值,描述返回的类型。
id 这是iOS5之前的
缺点:不能帮你进行编译检查(这里就涉及到oc的runTime), 当你调用一个Person 没有的方法时(能调用任何对象的方法),编译能通过,但运行时就会报错。
也不能使用点语法创建一个模型 #import "Person.h" // 用类方法快速创建一个 对象 [Person person]; // 编译时通过 运行时报错 [[Person person] setFrame:CGRectZero];
-------------
@implementation Person
+ (id)person{
return [[self alloc] init];
}instancetype ios5之后
instancetype:会自动识别当前类的对象,谁(Person)调用它(person),它就是谁的对象
它在编译时,就可以确定当前类对象 有没这个方法
缺点:和ID一样 还是无法确定返回值 类型(nstancetype可以返回和方法所在类相同类型的对象,但别人用的使用 是看不出来的 )
id 与 instancetype
①instancetype可以返回和方法所在类相同类型的对象,id只能返回未知类型的对象;
②instancetype只能作为返回值,不能像id那样作为参数,比如下面的写法
__kindof 既能确定返回值类型 也能 用于描述子类
虽然 如果用 当前类作为作为返回值 +(Person *)person{ } 是一劳永逸,既能编译时帮你检查,也能确定你的返回值。 但 当这个类 有子类时 SubPerson *person = [SubPerson person]; warn:Incompatible pointer types initializing 'SubPerson *' with an expression of type 'Person *' 会报Warn 子类调用父类的方法 生成的还是父类对象[Person person] 一样
使用__kindof 关键字修饰 就可以解决上面问题
@interface Person : NSObject // instancetype:会自动识别当前类的对象 // __kindof:表示当前类或者子类 + (__kindof Person *)person; -------- #import "Person.h" @implementation Person + (Person *)person{
return [[self alloc] init];
}id instancetype 在用于 子类调用父类方法时 不会产生这种warn(因为它们本来就没确定,而且这也是他们的优点吧)
-(nullable __kindof UITableViewCell )cellForRowAtIndexPath:(NSIndexPath )indexPath;
UITableView 也用__kindof, 因为我们经常要自定义cell,到时就没必要 强转类型啦