这两者直接看源码就好了,在NSObject.mm这个文件里面
+ (BOOL)isMemberOfClass:(Class)cls {
return self->ISA() == cls;
}
- (BOOL)isMemberOfClass:(Class)cls {
return [self class] == cls;
}
+ (BOOL)isKindOfClass:(Class)cls {
for (Class tcls = self->ISA(); tcls; tcls = tcls->superclass) {
if (tcls == cls) return YES;
}
return NO;
}
- (BOOL)isKindOfClass:(Class)cls {
for (Class tcls = [self class]; tcls; tcls = tcls->superclass) {
if (tcls == cls) return YES;
}
return NO;
}
+ (BOOL)isSubclassOfClass:(Class)cls {
for (Class tcls = self; tcls; tcls = tcls->superclass) {
if (tcls == cls) return YES;
}
return NO;
}
这就是五个定义,可以看的出来isMember就是判断是否是当前类,而isKindOf和isSubclassOfClass会考虑是否是父类
Person *person = [[Person alloc] init];
NSLog(@"%d",[person isKindOfClass:[Person class]]); //1
NSLog(@"%d",[person isKindOfClass:[NSObject class]]); //1
NSLog(@"%d",[person isMemberOfClass:[Person class]]); //1
NSLog(@"%d",[person isMemberOfClass:[NSObject class]]); //0
NSLog(@"%d",[Person isSubclassOfClass:[Person class]]); //1
NSLog(@"%d",[Person isSubclassOfClass:[NSObject class]]);//1
所以如果只想判断是否是Person就用isMember,如果是它的父类也行就用其它两个
|