再nullsafe的github界面里,它的介绍是:
NullSafe is a simple category on NSNull that returns nil for
unrecognised messages instead of throwing an exception.
看了源码始终不得要领。有哪位能帮忙说说其中原理?
#import <objc/runtime.h>
#import <Foundation/Foundation.h>
#ifndef NULLSAFE_ENABLED
#define NULLSAFE_ENABLED 1
#endif
#pragma GCC diagnostic ignored "-Wgnu-conditional-omitted-operand"
@implementation NSNull (NullSafe)
#if NULLSAFE_ENABLED
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
{
@synchronized([self class])
{
//look up method signature
NSMethodSignature *signature = [super methodSignatureForSelector:selector];
if (!signature)
{
//not supported by NSNull, search other classes
static NSMutableSet *classList = nil;
static NSMutableDictionary *signatureCache = nil;
if (signatureCache == nil)
{
classList = [[NSMutableSet alloc] init];
signatureCache = [[NSMutableDictionary alloc] init];
//get class list
int numClasses = objc_getClassList(NULL, 0);
Class *classes = (Class *)malloc(sizeof(Class) * (unsigned long)numClasses);
numClasses = objc_getClassList(classes, numClasses);
//add to list for checking
NSMutableSet *excluded = [NSMutableSet set];
for (int i = 0; i < numClasses; i++)
{
//determine if class has a superclass
Class someClass = classes[i];
Class superclass = class_getSuperclass(someClass);
while (superclass)
{
if (superclass == [NSObject class])
{
[classList addObject:someClass];
break;
}
[excluded addObject:NSStringFromClass(superclass)];
superclass = class_getSuperclass(superclass);
}
}
//remove all classes that have subclasses
for (Class someClass in excluded)
{
[classList removeObject:someClass];
}
//free class list
free(classes);
}
//check implementation cache first
NSString *selectorString = NSStringFromSelector(selector);
signature = signatureCache[selectorString];
if (!signature)
{
//find implementation
for (Class someClass in classList)
{
if ([someClass instancesRespondToSelector:selector])
{
signature = [someClass instanceMethodSignatureForSelector:selector];
break;
}
}
//cache for next time
signatureCache[selectorString] = signature ?: [NSNull null];
}
else if ([signature isKindOfClass:[NSNull class]])
{
signature = nil;
}
}
return signature;
}
}
- (void)forwardInvocation:(NSInvocation *)invocation
{
[invocation invokeWithTarget:nil];
}
#endif
@end
我不知道这个库,我稍微看了一下。
简单点说,当我们给一个NSNull对象发送消息的话,可能会崩溃(null是有内存的),而发送给nil的话,是不会崩溃的。
作者就是使用了这么一个原理,把发送给NSNull的而NSNull又无法处理的消息经过如下几步处理:
创建一个方法缓存,这个缓存会缓存项目中类的所有类名。
遍历缓存,寻找是否已经有可以执行此方法的类。
如果有的话,返回这个
NSMethodSignature
。如果没有的话,返回nil,接下来会走
forwardInvocation:
方法。[invocation invokeWithTarget:nil];
将消息转发给nil。那么,如何判断NSNull无法处理这个消息呢,在OC中,系统如果对某个实例发送消息之后,它(及其父类)无法处理(比如,没有这个方法等),系统就会发送
methodSignatureForSelector
消息,如果这个方法返回非空,那么就去执行返回的方法,如果为nil,则发送forwardInvocation
消息。这样就完成整个转发链了。
题外话:
一般来说,我们不应该在我们的项目中使用NSNull类(大部分NSNull类的来源来自于接口的返回),而使用nil,在来源上,就应该堵上(要么你解析到null进行处理,要么和你的服务端说,不要给我返回null)。