面试题一
在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_copyMethodList
、fxInstanceMethod_classToMetaclass
、fxClassMethod_classToMetaclass
和 fxIMP_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!
打印结果分析:
-
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
描述信息里面已经说明:由类实现,不由父类实现的任何实例方法
-
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
pClass
是FXPerson 类
,在FXPerson 类
中能够找到sayHello实例方法
,所以返回这个实例方法
metaClass
是FXPerson 元类
,在FXPerson 元类
中不能够找到sayHello实例方法
,所以返回NULL
pClass
是FXPerson 类
,在FXPerson 类
中不能够找到sayHappy实例方法
,所以返回NULL
metaClass
是FXPerson 元类
,在FXPerson 元类
中能够找到sayHappy实例方法
,所以返回这个实例方法
-
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
pClass
是FXPerson 类
,不是元类
,然后找元类 --> 根元类 --> 根类 --> nil
依次查找,最后返回NULL
metaClass
是FXPerson 元类
,在FXPerson 元类
中不能够找到sayHello实例方法
,所以返回NULL
pClass
是FXPerson 类
,不是元类
,然后找FXPerson 元类
,在FXPerson 元类
中能够找到sayHappy实例方法
,所以返回这个实例方法
metaClass
是FXPerson 元类
,在FXPerson 元类
中能够找到sayHappy实例方法
,所以返回这个实例方法
-
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
,则返回的函数指针将是运行时消息转发机制的一部分
pClass
是FXPerson 类
,在FXPerson 类
中能够找到sayHello实例方法
,所以返回一个imp函数指针
的地址metaClass
是FXPerson 元类
,在FXPerson 元类
中不能够找到sayHello实例方法
,所以进行了消息转发
pClass
是FXPerson 类
,在FXPerson 类
中不能够找到sayHappy实例方法
,所以进行了消息转发
metaClass
是FXPerson 元类
,在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;
}
网友评论