美文网首页
五、 OC底层面试题

五、 OC底层面试题

作者: Mjs | 来源:发表于2020-09-16 17:27 被阅读0次

    1.属性&成员变量&属性的区别

    属性(property):在OC中是通过@property开头定义,且是带下划线成员变量 + setter + getter方法的变量

    成员变量(ivar):在OC的类中{}中定义的,且没有下划线的变量

    实例变量:通过当前对象类型,具备实例化的变量,是一种特殊的成员变量,例如 NSObject、UILabel、UIButton等

    【附加】成员变量 和 实例变量什么区别?

    • 实例变量(即成员变量中的对象变量 就是 实例变量):以实例对象实例化来的,是一种特殊的成员变量

    • NSString 是常量类型, 因为不能添加属性,如果定义在类中的{}中,是成员变量

    • 成员变量中 除去基本数据类型、NSString,其他都是 实例变量(即可以添加属性的成员变量),实例变量主要是判断是不是对象

    2.类方法归属

    首先我们声明一个类

    @interface LGPerson : NSObject
    - (void)sayHello;
    + (void)sayHappy;
    @end
    
    @implementation LGPerson
    
    - (void)sayHello{
        NSLog(@"LGPerson say : Hello!!!");
    }
    + (void)sayHappy{
        NSLog(@"LGPerson say : Happy!!!");
    }
    
    @end
    
    

    然后写上测试代码

    
    void lgObjc_copyMethodList(Class pClass){
        unsigned int count = 0;
        Method *methods = class_copyMethodList(pClass, &count);
        for (unsigned int i=0; i < count; i++) {
            Method const method = methods[i];
            //获取方法名
            NSString *key = NSStringFromSelector(method_getName(method));
            
            LGLog(@"Method, name: %@", key);
        }
        free(methods);
    }
    
    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));
        // 元类 为什么有 sayHappy 类方法 0 1
        //
        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__);
    }
    
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            // insert code here...
            // 0x0000000100000000
            // LGTeacher *teacher = [LGTeacher alloc];
    
            LGPerson *person = [LGPerson alloc];
            Class pClass     = object_getClass(person);
            lgObjc_copyMethodList(pClass);///获取方法列表
    
            lgInstanceMethod_classToMetaclass(pClass);
            lgClassMethod_classToMetaclass(pClass);
        }
        return 0;
    }
    

    打印结果

    Method, name: sayHello
    lgInstanceMethod_classToMetaclass - 0x1000031b0-0x0-0x0-0x100003148
    lgClassMethod_classToMetaclass-0x0-0x0-0x100003148-0x100003148
    

    class_getInstanceMethod:

        auto const methods = cls->data()->methods();
    

    class_getInstanceMethod中我们获取的是data()的methods();根据之前的文章我们就可以很容易的理解实例方法放在类中,类方法放在元类中
    class_getClassMethod:

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

    所以class_getClassMethod最后获取的都是元类的实例方法

    3.iskindOfClass & isMemberOfClass

            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 :1
     re2 :0
     re3 :0
     re4 :0
    
    /**实例方法调用*/
      re5 :1
     re6 :1
     re7 :1
     re8 :1
    

    在调用类方法

    + (BOOL)isKindOfClass:(Class)cls {
        for (Class tcls = self->ISA(); tcls; tcls = tcls->superclass) {
            if (tcls == cls) return YES;
        }
        return NO;
    }
    

    isKindOfClass会先找到类的元类,在和cls进行对比,如果没找到就会找superclass,
    值得注意的是NSObject会被llvm转义优化

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

    所以只有re1正确

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

    isMemberOfClass只会和元类和cls对比,所以都不正确

    实例方法中:
    isKindOfClass:

    - (BOOL)isKindOfClass:(Class)cls {
        for (Class tcls = [self class]; tcls; tcls = tcls->superclass) {
            if (tcls == cls) return YES;
        }
        return NO;
    }
    

    会从当前类,然后一层层往上找父类,所以都正确

    isMemberOfClass:

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

    isMemberOfClass只会将当前类来比较,所以也都正确

    继承图.png
    这个时候看这张图就能完全解释了

    相关文章

      网友评论

          本文标题:五、 OC底层面试题

          本文链接:https://www.haomeiwen.com/subject/psocyktx.html