Runtime运行时

isKindOfClass和isMemberOfClass分析

2019-12-31  本文已影响0人  海浪萌物

有一道经典面试题关于isKindOfClass和isMemberOfClass

代码:

        BOOL re1 = [(id)[NSObject class] isKindOfClass:[NSObject class]];       // 1
        BOOL re2 = [(id)[NSObject class] isMemberOfClass:[NSObject class]];     // 0
        BOOL re3 = [(id)[LGPerson class] isKindOfClass:[LGPerson class]];       // 0
        BOOL re4 = [(id)[LGPerson class] isMemberOfClass:[LGPerson class]];     // 0
        NSLog(@" re1 :%hhd\n re2 :%hhd\n re3 :%hhd\n re4 :%hhd\n",re1,re2,re3,re4);

        BOOL re5 = [(id)[NSObject alloc] isKindOfClass:[NSObject class]];       // 1
        BOOL re6 = [(id)[NSObject alloc] isMemberOfClass:[NSObject class]];     // 1
        BOOL re7 = [(id)[LGPerson alloc] isKindOfClass:[LGPerson class]];       // 1
        BOOL re8 = [(id)[LGPerson alloc] isMemberOfClass:[LGPerson class]];     // 1
        NSLog(@" re5 :%hhd\n re6 :%hhd\n re7 :%hhd\n re8 :%hhd\n",re5,re6,re7,re8);

输出结果:

2019-12-31 14:49:22.734091+0800 LGTest[35237:2807868] 
 re1 :1
 re2 :0
 re3 :0
 re4 :0
2019-12-31 14:49:22.735580+0800 LGTest[35237:2807868] 
 re5 :1
 re6 :1
 re7 :1
 re8 :1

为什么结果是这样呢?

先放一个isa的指针图:


isa流程图.png
+ (Class)class {
    return self;
}
Class object_getClass(id obj)
{
    if (obj) return obj->getIsa();
    else return Nil;
}
+ (BOOL)isKindOfClass:(Class)cls {
  //
    for (Class tcls = object_getClass((id)self); tcls; tcls = tcls->superclass) {
        if (tcls == cls) return YES;
    }
    return NO;
}

我们发现isKindOfClass是循环不断获取self的isa指针以及父类的isa指针指向和cls做对比,通过上面isa的指向图,我们可以拿到下面代码逻辑:

BOOL re1 = [(id)[NSObject class] isKindOfClass:[NSObject class]];       // 1

先拿到NSObject的元类跟NSObject比,因为NSObject元类的父类是NSObject,所以结果是YES

BOOL re3 = [(id)[LGPerson class] isKindOfClass:[LGPerson class]];       // 0

而遍历LGPerson的父类的isa却是拿LGPerson的元类、NSObject的元类、NSObject和LGPerson进行对比,所以,结果是NO

+ (BOOL)isMemberOfClass:(Class)cls {
    return object_getClass((id)self) == cls;
}

我们发现isMemberOfClass仅仅是拿到当前self的isa指针指向和cls对比,然后我们分析测试代码逻辑:

        BOOL re2 = [(id)[NSObject class] isMemberOfClass:[NSObject class]];     // 0

NSObject的元类和NSObject匹配,不成立

        BOOL re4 = [(id)[LGPerson class] isMemberOfClass:[LGPerson class]];     // 0

LGPerson的元类和LGPerson匹配,不成立

- (BOOL)isKindOfClass:(Class)cls {
    // 类  - NSObject 类 vs 父类 nil
    for (Class tcls = [self class]; tcls; tcls = tcls->superclass) {
        if (tcls == cls) return YES;
    }
    return NO;
}
- (BOOL)isMemberOfClass:(Class)cls {
    return [self class] == cls;
}

我们可以发现对于对象方法,只是拿到对象的类和cls对比,所以下面四个结果都是YES。

上一篇下一篇

猜你喜欢

热点阅读