美文网首页
iOS全局解决 字典数组崩溃问题

iOS全局解决 字典数组崩溃问题

作者: __May__ | 来源:发表于2019-01-03 17:49 被阅读0次

当我们不小心将空的对象加入到数组和字典中就会崩溃。这里给出使用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;

}


相关文章

网友评论

      本文标题:iOS全局解决 字典数组崩溃问题

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