BOOL res1 = [(id)[NSObject class] isKindOfClass:[NSObject class]];//YES
在runtime源码中:
+ (BOOL)isKindOfClass:(Class)cls {
for(Class tcls = object_getClass((id)self); tcls; tcls = tcls->superclass) {
if(tcls == cls)return YES;
}
return NO;
}
调用类方法:cls为NSObject,而 tcls是第一次先调用object_getClass((id)self)返回objc_class中shiftcls 的isa指针,它指向元类,继续循环在父类查找...一直到Rootclass的元类它的父类指向Rootclass类(NSObject),而cls为NSObject 所以相等
BOOL res2 = [(id)[NSObject class]isMemberOfClass:[NSObject class]];
在runtime源码:
+ (BOOL)isMemberOfClass:(Class)cls {
return object_getClass((id)self) == cls;
}
调用类方法:object_getClass((id)self)通过 isa_t的isa.bits进行位运算获取 isa指针指向元类而cls为NSObject类所以不想等
//BOOL res3 = [(id)[Sark class] isKindOfClass:[Sark class]];
runtime源码:
+ (BOOL)isKindOfClass:(Class)cls {
for(Class tcls =object_getClass((id)self); tcls; tcls = tcls->superclass) {
if(tcls == cls)return YES;
}
return NO;
}
调用类方法:通过object_getClass((id)self)指向元类与cls不想等,继续循环在父类中...一直走到RootClass元类它的父类指向NSObject截止,而cls为Sark,与NSObject不相等所以返回NO
//BOOL res4 = [(id)[Sark class] isMemberOfClass:[Sark class]];
runtime源码:
+ (BOOL)isMemberOfClass:(Class)cls {
return object_getClass((id)self) == cls;
}
调用类方法:object_getClass((id)self)获取指向元类,与NSObject不相等返回NO。
网友评论