isa相关面试题分析

面试题

1.内省方法isKindOfClassisMemberOfClass

        BOOL re1 = [(id)[NSObject class] isKindOfClass:[NSObject class]];       //
        BOOL re2 = [(id)[NSObject class] isMemberOfClass:[NSObject class]];     //
        BOOL re3 = [(id)[LGPerson class] isKindOfClass:[LGPerson class]];       //
        BOOL re4 = [(id)[LGPerson class] isMemberOfClass:[LGPerson class]];     //
        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]];       //
        BOOL re6 = [(id)[NSObject alloc] isMemberOfClass:[NSObject class]];     //
        BOOL re7 = [(id)[LGPerson alloc] isKindOfClass:[LGPerson class]];       //
        BOOL re8 = [(id)[LGPerson alloc] isMemberOfClass:[LGPerson class]];     //
        NSLog(@" re5 :%hhd\n re6 :%hhd\n re7 :%hhd\n re8 :%hhd\n",re5,re6,re7,re8);

分析

首先注意的点re1,re2,re3,re4isKindOfClassisMemberOfClass是类方法
re5,re6,re7,re8isKindOfClassisMemberOfClass是实例方法

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->superclass) {
            if (tcls == otherClass) return YES;
        }
        return NO;
    }
#endif
    return ((BOOL(*)(id, SEL, Class))objc_msgSend)(obj, @selector(isKindOfClass:), otherClass);
}

obj传入的是类,Class cls = obj->getIsa()找到元类,拿到类的isa(元类),递归superclass,和当前比较是否相等
obj传入对象, Class cls = obj->getIsa()拿类实例对象class,递归superclass,和当前比较是否相等

+ (BOOL)isMemberOfClass:(Class)cls {
    return self->ISA() == cls;
}

+ isMemberOfClass 是先拿到类的isa(元类)和当前比较是否相等

- (BOOL)isMemberOfClass:(Class)cls {
    return [self class] == cls;
}

-isMemberOfClass 是拿类实例对象class,和当前比较是否相等

所以上面输出re1=YES, re2 =NO, re3 =NO,re4 =NO, re5=YES, re6 =YES, re7=YES, re8=YES

re1[NSObject class] 的 根元类 继承 NSObject 所以re1=yes
re2[NSObject class]元类不等于[NSObject class]
re3[LGPerson class]的根元类是NSObject[LGPerson class]不相等
re4[LGPerson class] 元类与[LGPerson class]不相等

re5~re8比较的是class 所以都是yes

2.类方法和实例方法

创建LGPerson类 类里面包含两个方法,然后在main函数打印下面几个方法会输出什么

@interface LGPerson : NSObject
- (void)sayHello;
+ (void)sayHappy;
@end
@implementation LGPerson
- (void)sayHello{
    NSLog(@"LGPerson say : Hello!!!");
}
+ (void)sayHappy{
    NSLog(@"LGPerson say : Happy!!!");
}
@end
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        LGPerson *person = [LGPerson alloc];
        Class pClass = object_getClass(person);
//        lgInstanceMethod_classToMetaclass(pClass);
//        lgClassMethod_classToMetaclass(pClass);
    }
    return 0;
}

void lgInstanceMethod_classToMetaclass(Class pClass){
    const char *className = class_getName(pClass);
    Class metaClass = objc_getMetaClass(className);
    
    Method method1 = class_getInstanceMethod(pClass, @selector(sayHello));
    Method method2 = class_getInstanceMethod(metaClass, @selector(sayHello));

    Method method3 = class_getInstanceMethod(pClass, @selector(sayHappy));
    Method method4 = class_getInstanceMethod(metaClass, @selector(sayHappy));
    
    LGLog(@"%s - %p-%p-%p-%p",__func__,method1,method2,method3,method4);
}

void lgClassMethod_classToMetaclass(Class pClass){
    
    const char *className = class_getName(pClass);
    Class metaClass = objc_getMetaClass(className);
    
    Method method1 = class_getClassMethod(pClass, @selector(sayHello));
    Method method2 = class_getClassMethod(metaClass, @selector(sayHello));

    Method method3 = class_getClassMethod(pClass, @selector(sayHappy));
    Method method4 = class_getClassMethod(metaClass, @selector(sayHappy));
    LGLog(@"%s-%p-%p-%p-%p",__func__,method1,method2,method3,method4);
}

void lgIMP_classToMetaclass(Class pClass){
    
    const char *className = class_getName(pClass);
    Class metaClass = objc_getMetaClass(className);

    // - (void)sayHello;
    // + (void)sayHappy;
    IMP imp1 = class_getMethodImplementation(pClass, @selector(sayHello));
    IMP imp2 = class_getMethodImplementation(metaClass, @selector(sayHello));

    IMP imp3 = class_getMethodImplementation(pClass, @selector(sayHappy));
    IMP imp4 = class_getMethodImplementation(metaClass, @selector(sayHappy));

    NSLog(@"%p-%p-%p-%p",imp1,imp2,imp3,imp4);
    NSLog(@"%s",__func__);
}

void lgInstanceMethod_classToMetaclass(Class pClass)输出0x1000031b0-0x0-0x0-0x100003148可以看出method1method4能找到方法,method2method3没有找到方法
method4 sayHappy 在元类(metaClass)中是实例方法

objc_getMetaClass得到类的元类
class_getInstanceMethod 作用是得到类的实例方法
说明实例方法存在类中,类方法存在元类中

class_getClassMethod作用是的到类中的类方法

void lgClassMethod_classToMetaclass(Class pClass) 输出0x0-0x0-0x100003148-0x100003148, method1和method2没有找类方法

method3存在类方法
method4 也能找到方法, 为什么元类里能找到类方法呢?
分析源码

Method class_getClassMethod(Class cls, SEL sel)
{
    if (!cls  ||  !sel) return nil;
    return class_getInstanceMethod(cls->getMeta(), sel);
}
Class getMeta() {
  if (isMetaClass()) return (Class)this;
  else return this->ISA();
}

通过源码可以看出 return class_getInstanceMethod(cls->getMeta(), sel);,
getMeta()如果是元类直接返回类,所以元类也能找到类方法

lgIMP_classToMetaclass 输出0x100001d10-0x7fff684c9580-0x7fff684c9580-0x100001d40

实例方法是在类中,类方法存在元类中,那么imp1和imp4 有值
imp2和imp3为什么能找到imp呢?
看源码

IMP class_getMethodImplementation(Class cls, SEL sel)
{
    IMP imp;

    if (!cls  ||  !sel) return nil;

    imp = lookUpImpOrNil(nil, sel, cls, LOOKUP_INITIALIZE | LOOKUP_RESOLVER);

    // Translate forwarding function to C-callable external version
    if (!imp) {
        return _objc_msgForward;
    }

    return imp;
}

imp2和imp3 找不到imp走_objc_msgForward消息转发流程

你可能感兴趣的:(isa相关面试题分析)