美文网首页
iOS经典面试题分析

iOS经典面试题分析

作者: 奉灬孝 | 来源:发表于2020-09-15 22:48 被阅读0次

面试题一

iOS类的结构分析中的探索中,我们知道了实例方法 存储在中,类方法存储在元类中,接下来我们来分析一下面试题一:

FXPerson类文件

#import <Foundation/Foundation.h>

@interface FXPerson : NSObject
- (void)sayHello;
+ (void)sayHappy;

@end

#import "LGPerson.h"

@implementation LGPerson

- (void)sayHello{
    NSLog(@"LGPerson say : Hello!!!");
}

+ (void)sayHappy{
    NSLog(@"LGPerson say : Happy!!!");
}

@end

fxObjc_copyMethodListfxInstanceMethod_classToMetaclassfxClassMethod_classToMetaclassfxIMP_classToMetaclass方法

void fxObjc_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 fxInstanceMethod_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 fxClassMethod_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 fxIMP_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(@"%s-%p-%p-%p-%p",__func__,imp1,imp2,imp3,imp4);
}

面试题:分析各个函数的打印结果

FXPerson *person = [FXPerson alloc];
Class pClass     = object_getClass(person);
fxObjc_copyMethodList(pClass);

fxInstanceMethod_classToMetaclass(pClass);
fxClassMethod_classToMetaclass(pClass);
fxIMP_classToMetaclass(pClass);
NSLog(@"Hello, World!");

打印结果:

// 打印结果
Method, name: sayHello
lgInstanceMethod_classToMetaclass - 0x1000031b0-0x0-0x0-0x100003148
lgClassMethod_classToMetaclass-0x0-0x0-0x100003148-0x100003148
lgIMP_classToMetaclass-0x100001d10-0x7fff66861580-0x7fff66861580-0x100001d40
2020-09-15 18:46:31.544820+0800 002-类方法归属分析[18280:408952] Hello, World!

打印结果分析:

  1. fxObjc_copyMethodLists函数里面的class_copyMethodList函数
/** 
 * Describes the instance methods implemented by a class.
 * 
 * @param cls The class you want to inspect.
 * @param outCount On return, contains the length of the returned array. 
 *  If outCount is NULL, the length is not returned.
 * 
 * @return An array of pointers of type Method describing the instance methods 
 *  implemented by the class—any instance methods implemented by superclasses are not included. 
 *  The array contains *outCount pointers followed by a NULL terminator. You must free the array with free().
 * 
 *  If cls implements no instance methods, or cls is Nil, returns NULL and *outCount is 0.
 * 
 * @note To get the class methods of a class, use \c class_copyMethodList(object_getClass(cls), &count).
 * @note To get the implementations of methods that may be implemented by superclasses, 
 *  use \c class_getInstanceMethod or \c class_getClassMethod.
 */
OBJC_EXPORT Method _Nonnull * _Nullable
class_copyMethodList(Class _Nullable cls, unsigned int * _Nullable outCount) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

return 描述信息里面已经说明:由类实现,不由父类实现的任何实例方法

  1. fxInstanceMethod_classToMetaclass 函数里面的class_getInstanceMethod 函数
/** 
 * Returns a specified instance method for a given class.
 * 
 * @param cls The class you want to inspect.
 * @param name The selector of the method you want to retrieve.
 * 
 * @return The method that corresponds to the implementation of the selector specified by 
 *  \e name for the class specified by \e cls, or \c NULL if the specified class or its 
 *  superclasses do not contain an instance method with the specified selector.
 *
 * @note This function searches superclasses for implementations, whereas \c class_copyMethodList does not.
 */
OBJC_EXPORT Method _Nullable
class_getInstanceMethod(Class _Nullable cls, SEL _Nonnull name)
    OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);

return 描述信息里面已经说明:如果在传入的类或者类的父类中没有找到指定的实例方法,则返回NULL

  • pClassFXPerson 类 ,在 FXPerson 类 中能够找到 sayHello实例方法,所以返回这个 实例方法
  • metaClassFXPerson 元类 ,在 FXPerson 元类 中不能够找到 sayHello实例方法,所以返回 NULL
  • pClassFXPerson 类 ,在 FXPerson 类 中不能够找到 sayHappy实例方法,所以返回 NULL
  • metaClassFXPerson 元类 ,在 FXPerson 元类 中能够找到 sayHappy实例方法,所以返回这个 实例方法
  1. fxClassMethod_classToMetaclass 函数里面的class_getClassMethod 函数
/** 
 * Returns a pointer to the data structure describing a given class method for a given class.
 * 
 * @param cls A pointer to a class definition. Pass the class that contains the method you want to retrieve.
 * @param name A pointer of type \c SEL. Pass the selector of the method you want to retrieve.
 * 
 * @return A pointer to the \c Method data structure that corresponds to the implementation of the 
 *  selector specified by aSelector for the class specified by aClass, or NULL if the specified 
 *  class or its superclases do not contain an instance method with the specified selector.
 *
 * @note Note that this function searches superclasses for implementations, 
 *  whereas \c class_copyMethodList does not.
 */
OBJC_EXPORT Method _Nullable
class_getClassMethod(Class _Nullable cls, SEL _Nonnull name)
    OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);

然后再来看该方法的源码实现,可以得出 class_getClassMethod 的实现是获取类的类方法,其本质就是获取 元类实例方法 ,最终还是会走到 class_getInstanceMethod ,但是在这里需要注意的一点是:在 getMeta源码 中,如果判断出 cls元类,那么就不会再继续往下递归查找,会直接返回 this,其目的是为了防止元类的无限递归查找

/***********************************************************************
* class_getClassMethod.  Return the class method for the specified
* class and selector.
**********************************************************************/
Method class_getClassMethod(Class cls, SEL sel)
{
    if (!cls  ||  !sel) return nil;

    return class_getInstanceMethod(cls->getMeta(), sel);
}

// NOT identical to this->ISA when this is a metaclass
Class getMeta() {
    if (isMetaClass()) return (Class)this;
    else return this->ISA();
}

return 描述信息里面已经说明:如果在传入的类或者类的父类中没有找到指定的类方法,则返回NULL

  • pClassFXPerson 类 ,不是 元类 ,然后找 元类 --> 根元类 --> 根类 --> nil依次查找,最后返回 NULL
  • metaClassFXPerson 元类 ,在 FXPerson 元类 中不能够找到 sayHello实例方法,所以返回 NULL
  • pClassFXPerson 类 ,不是 元类,然后找 FXPerson 元类,在 FXPerson 元类 中能够找到 sayHappy实例方法,所以返回这个 实例方法
  • metaClassFXPerson 元类 ,在 FXPerson 元类 中能够找到 sayHappy实例方法,所以返回这个 实例方法
  1. fxIMP_classToMetaclass 函数里面的class_getMethodImplementation 函数
/** 
 * Returns the function pointer that would be called if a 
 * particular message were sent to an instance of a class.
 * 
 * @param cls The class you want to inspect.
 * @param name A selector.
 * 
 * @return The function pointer that would be called if \c [object name] were called
 *  with an instance of the class, or \c NULL if \e cls is \c Nil.
 *
 * @note \c class_getMethodImplementation may be faster than \c method_getImplementation(class_getInstanceMethod(cls, name)).
 * @note The function pointer returned may be a function internal to the runtime instead of
 *  an actual method implementation. For example, if instances of the class do not respond to
 *  the selector, the function pointer returned will be part of the runtime's message forwarding machinery.
 */
OBJC_EXPORT IMP _Nullable
class_getMethodImplementation(Class _Nullable cls, SEL _Nonnull name) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

return 描述信息里面已经说明:该方法最终将会返回调用函数的指针,返回的函数指针可能是一个指向 runtime 内部的函数,而不一定是方法的实际实现。如果类实例无法响应 selector,则返回的函数指针将是运行时消息转发机制的一部分

  • pClassFXPerson 类 ,在 FXPerson 类 中能够找到 sayHello实例方法,所以返回一个 imp函数指针 的地址
  • metaClassFXPerson 元类 ,在 FXPerson 元类 中不能够找到 sayHello实例方法,所以进行了 消息转发
  • pClassFXPerson 类 ,在 FXPerson 类 中不能够找到 sayHappy实例方法,所以进行了 消息转发
  • metaClassFXPerson 元类 ,在 FXPerson 元类 中能够找到 sayHappy实例方法,所以返回一个 imp函数指针 的地址

面试题二: 分析打印结果

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);

打印结果:

2020-09-15 22:38:50.139130+0800 KCObjc[23825:541164]  
 re1 :1
 re2 :0
 re3 :0
 re4 :0
2020-09-15 22:38:50.139506+0800 KCObjc[23825:541164]  
 re5 :1
 re6 :1
 re7 :1
 re8 :1

按照惯例,先上isa/superclass经典关系图:

实例对象、类、元类关系图

打印结果分析:

- (BOOL)isKindOfClass 对象方法

第一次是获取对象类 与 传入类对比,如果不相等,后续对比是继续获取上次 类的父类 与传入类进行对比

  • re5: NSObject对象的类 - NSObject 类NSObject 类 相等,返回 1
  • re7: LGPerson 对象的类 - LGPerson 类LGPerson 类 相等,返回 1
- (BOOL)isKindOfClass:(Class)cls {
    for (Class tcls = [self class]; tcls; tcls = tcls->superclass) {
        if (tcls == cls) return YES;
    }
    return NO;
}

+ (BOOL)isKindOfClass 类方法

第一次比较是 获取类的元类 与 传入类对比,再次之后的对比是获取上次结果的父类 与 传入 类进行对比

  • re1: NSObject 类NSObject 元类 不相等,然后再比较 NSObject 类NSObject 元类的父类,相等,返回 1
  • re3: LGPerson 类LGPerson 元类 不相等,然后再比较 LGPerson 类LGPerson 元类的父类 - LGPerson 根元类 不相等,然后再比较 LGPerson 类LGPerson 根元类 的父类 - NSObject 类 不相等,然后再比较 LGPerson 类NSObject 类 的父类 - nil 不相等,返回 0
+ (BOOL)isKindOfClass:(Class)cls {
    // 类 vs 元类
    // 根元类 vs NSObject
    // NSObject vs NSObject
    // LGPerson vs 元类 (根元类) (NSObject)
    for (Class tcls = self->ISA(); tcls; tcls = tcls->superclass) {
        if (tcls == cls) return YES;
    }
    return NO;
}

- (BOOL)isMemberOfClass 对象方法

获取对象的类,与 传入类对比

  • re6: NSObject 对象的类 - NSObject 类NSObject 类 相等,返回 1
  • re8: LGPerson 对象的类 - LGPerson 类LGPerson 类 相等,返回 1
- (BOOL)isMemberOfClass:(Class)cls {
    return [self class] == cls;
}

+ (BOOL)isMemberOfClass 类方法

获取类的元类,与 传入类对比

  • re2: NSObject 类NSObject 元类 不相等,返回 0
  • re4: LGPerson 类LGPerson 元类 不相等,返回 0
+ (BOOL)isMemberOfClass:(Class)cls {
    return self->ISA() == cls;
}

相关文章

  • iOS经典面试题总结--内存管理

    iOS经典面试题总结--内存管理 iOS经典面试题总结--内存管理

  • IOS面试(2018)

    经典面试题 非技术面试题 C面试题1 C面试题2 C面试题3 iOS笔试题01 iOS笔试题02 iOS笔试题03...

  • ios-面试题链接(四)

    经典面试题 非技术面试题 C面试题1 C面试题2 C面试题3 iOS笔试题01 iOS笔试题02 iOS笔试题03...

  • 2018 iOS面试题系列

    经典面试题 非技术面试题 C面试题1 C面试题2 C面试题3 iOS笔试题01 iOS笔试题02 iOS笔试题03...

  • iOS底层面试题--RunLoop

    什么是RunLoop? iOS底层面试题--RunLoop RunLoop面试题分析

  • iOS面试

    iOS面试题阿里、字节:一套高效的iOS面试题iOS源码分析 1、谈下iOS开发中知道的哪些锁? 哪个性能最差?S...

  • iOS经典面试题分析

    面试题一 在iOS类的结构分析中的探索中,我们知道了实例方法 存储在类中,类方法存储在元类中,接下来我们来分析一下...

  • BAHome:iOS 面试题收藏录

    BAHome:iOS 面试题收藏录 由于很多同学在面试中总是被噱,群里很多同学都要求整理一份经典的 iOS 面试题...

  • iOS经典Crash分析与总结

    iOS经典Crash分析与总结

  • iOS面试题大全

    iOS面试题大全-点亮你iOS技能树 iOS 事件处理机制与图像渲染过程 iOS界面渲染流程分析 wechat ...

网友评论

      本文标题:iOS经典面试题分析

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