BOOL result1 = [[NSObject class] isKindOfClass:[NSObject class]];
BOOL result2 = [[NSObject class] isMemberOfClass:[NSObject class]];
BOOL result3 = [[RYModel class] isKindOfClass:[RYModel class]];
BOOL result4 = [[RYModel class] isMemberOfClass:[RYModel class]];
NSLog(@"Class\n %hhd \n %hhd \n %hhd \n %hhd", result1, result2, result3, result4);
BOOL result5 = [[NSObject alloc] isKindOfClass:[NSObject class]];
BOOL result6 = [[NSObject alloc] isMemberOfClass:[NSObject class]];
BOOL result7 = [[RYModel alloc] isKindOfClass:[RYModel class]];
BOOL result8 = [[RYModel alloc] isMemberOfClass:[RYModel class]];
NSLog(@"Instacne\n %hhd \n %hhd \n %hhd \n %hhd", result5, result6, result7, result8);
2021-06-27 08:29:10.567002+0800 IsKindOf[62355:691675] Class
1
0
0
0
2021-06-27 08:29:10.567836+0800 IsKindOf[62355:691675] Instacne
1
1
1
1
+ (BOOL)isKindOfClass:(Class)cls {
for (Class tcls = self->ISA(); tcls; tcls = tcls->getSuperclass()) {
if (tcls == cls) return YES;
}
return NO;
}
- (BOOL)isKindOfClass:(Class)cls {
for (Class tcls = [self class]; tcls; tcls = tcls->getSuperclass()) {
if (tcls == cls) return YES;
}
return NO;
}
+ (BOOL)isMemberOfClass:(Class)cls {
return self->ISA() == cls;
}
- (BOOL)isMemberOfClass:(Class)cls {
return [self class] == cls;
}
-> 0x10000381c <+332>: mov rdi, qword ptr [rip + 0x4f7d] ; (void *)0x0000000100008808: RYModel
0x100003823 <+339>: call 0x100003b42 ; symbol stub for: objc_opt_class
0x100003828 <+344>: mov rbx, rax
0x10000382b <+347>: mov rdi, qword ptr [rip + 0x4f6e] ; (void *)0x0000000100008808: RYModel
0x100003832 <+354>: call 0x100003b42 ; symbol stub for: objc_opt_class
0x100003837 <+359>: mov rdi, rbx
0x10000383a <+362>: mov rsi, rax
0x10000383d <+365>: call 0x100003b48 ; symbol stub for: objc_opt_isKindOfClass
3.2 objc_opt_isKindOfClass
// Calls [obj isKindOfClass]
BOOL
objc_opt_isKindOfClass(id obj, Class otherClass)
{
#if __OBJC2__
// 判空
if (slowpath(!obj)) return NO;
Class cls = obj->getIsa();
if (fastpath(!cls->hasCustomCore())) {
for (Class tcls = cls; tcls; tcls = tcls->getSuperclass()) {
if (tcls == otherClass) return YES;
}
return NO;
}
#endif
// 这里可以忽略
return ((BOOL(*)(id, SEL, Class))objc_msgSend)(obj, @selector(isKindOfClass:), otherClass);
}