isKindOfClass与isMemberOfClass比较

通过runtime源码分析

代码块

+ (Class)class {
    return self;
}

- (Class)class {
    return object_getClass(self);
}

+ (Class)superclass {
    return self->superclass;
}

- (Class)superclass {
    return [self class]->superclass;
}

+ (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 result1 = [[NSObject class] isKindOfClass:[NSObject class]];
    BOOL result2 = [[NSObject class] isMemberOfClass:[NSObject class]];
    BOOL result3 = [[Person class] isKindOfClass:[Person class]];
    BOOL result4 = [[Person class] isMemberOfClass:[Person class]];
    BOOL result5 = [[Person class] isKindOfClass:[NSObject class]];


   
    NSLog(@"%d-%d-%d-%d-%d",result1,result2,result3,result4,result5);

输出结果:2021-11-07 11:52:01.460879+0800 TestClass[1483:52222] 1-0-0-0-1

91635690535_.pic_hd.jpg

通过源码分析得出:
iskindOfClass类方法:当前类是否与元类或者元类的父类是否相等(只要有相等的就返回,有一个递归的过程)。
iskindOfClass实例方法:当前类(或者实例)是否与当前类,元类或者元类的父类是否相等(只要有相等的就返回,有一个递归的过程)。
isMemeberOfClass类方法:当前类的元类与当前类是否相等
isMemeberOfClass实例方法:当前类与类的比较。

你可能感兴趣的:(isKindOfClass与isMemberOfClass比较)