當(dāng)我們不小心將空的對(duì)象加入到數(shù)組和字典中就會(huì)崩潰瘪松。這里給出使用runtime機(jī)制预明,做安全保護(hù)炎咖,代碼如下
字典
@implementationNSDictionary (utils)
+ (void)load {?
??//賦值時(shí)安全保護(hù)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);
//可變字典添加時(shí)安全保護(hù)
?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;
??? }
}
數(shù)組
@implementationNSArray (utils)
+ (void)load {
//數(shù)組初始化
? ?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);
//數(shù)組越界保護(hù)
? ? fromMethod =class_getInstanceMethod(objc_getClass("__NSArrayI"),@selector(objectAtIndex:));
??? toMethod =class_getInstanceMethod(objc_getClass("__NSArrayI"),@selector(em_objectAtIndex:));
???method_exchangeImplementations(fromMethod, toMethod);
?//可變數(shù)組
? ?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;
}