当我们不小心将空的对象加入到数组和字典中就会崩溃。这里给出使用runtime机制,做安全保护,代码如下
字典
@implementationNSDictionary (utils)
+ (void)load {
//赋值时安全保护Method fromMethod=class_getInstanceMethod(objc_getClass("__NSPlaceholderDictionary"),@selector(initWithObjects:forKeys:count:));
Method toMethod =class_getInstanceMethod(objc_getClass("__NSPlaceholderDictionary"),@selector(em_initWithObjects:forKeys:count:));
method_exchangeImplementations(fromMethod, toMethod);
//可变字典添加时安全保护
fromMethod =class_getInstanceMethod(objc_getClass("__NSDictionaryM"),@selector(setObject:forKey:));
toMethod =class_getInstanceMethod(objc_getClass("__NSDictionaryM"),@selector(em_setObject:forKey:));
method_exchangeImplementations(fromMethod, toMethod);
}
- (instancetype)em_initWithObjects:(id _Nonnullconst[])objects forKeys:(id<NSCopying> _Nonnullconst[])keys count:(NSUInteger)cnt
{
NSInteger index =0;
id newObjects[cnt];
id<NSCopying> newKeys[cnt];
for(inti=0; i<cnt; i++) {
id object = objects[i];
id<NSCopying> key = keys[i];
if(object) {
newObjects[index] = object;
newKeys[index] = key;
index++;
}
}
cnt = index;
return[self em_initWithObjects:newObjectsforKeys:newKeyscount:cnt];
}
- (void)em_setObject:(id)emObject forKey:(NSString*)key {
if(emObject && key) {
[self em_setObject:emObjectforKey:key];
}
}
- (id)objectValueForKey:(NSString*)key
{
idvalue = [self valueForKey:key];
if((!value || [value isKindOfClass:[NSNullclass]]))
{
returnnil;
}
else
{
returnvalue;
}
}
数组
@implementationNSArray (utils)
+ (void)load {
//数组初始化
Method fromMethod =class_getInstanceMethod(objc_getClass("__NSPlaceholderArray"),@selector(initWithObjects:count:));
Method toMethod =class_getInstanceMethod(objc_getClass("__NSPlaceholderArray"),@selector(em_initWithObjects:count:));
method_exchangeImplementations(fromMethod, toMethod);
//数组越界保护
fromMethod =class_getInstanceMethod(objc_getClass("__NSArrayI"),@selector(objectAtIndex:));
toMethod =class_getInstanceMethod(objc_getClass("__NSArrayI"),@selector(em_objectAtIndex:));
method_exchangeImplementations(fromMethod, toMethod);
//可变数组
fromMethod =class_getInstanceMethod(objc_getClass("__NSArrayM"),@selector(addObject:));
toMethod =class_getInstanceMethod(objc_getClass("__NSArrayM"),@selector(em_addObject:));
method_exchangeImplementations(fromMethod, toMethod);
fromMethod =class_getInstanceMethod(objc_getClass("__NSArrayM"),@selector(insertObject:atIndex:));
toMethod =class_getInstanceMethod(objc_getClass("__NSArrayM"),@selector(em_insertObject:atIndex:));
method_exchangeImplementations(fromMethod, toMethod);
fromMethod =class_getInstanceMethod(objc_getClass("__NSArrayM"),@selector(replaceObjectAtIndex:withObject:));
toMethod =class_getInstanceMethod(objc_getClass("__NSArrayM"),@selector(em_replaceObjectAtIndex:withObject:));
method_exchangeImplementations(fromMethod, toMethod);
}
- (instancetype)em_initWithObjects:(id _Nonnullconst[])objects count:(NSUInteger)cnt
{
id newObjects[cnt];
NSInteger index =0;
for(inti=0; i<cnt; i++) {
idobject = objects[i];
if(object) {
newObjects[index] = object;
index++;
}
}
cnt = index;
return[self em_initWithObjects:newObjectscount:cnt];
}
- (id)em_objectAtIndex:(NSUInteger)index
{
if(index<self.count) {
return[self em_objectAtIndex:index];
}
return nil;
}
网友评论