1.
The above method can already avoid crash, in order to avoid redundant code, write a Nsarray classification, use runtime to replace Nsarray object method Objectatindex:, here to judge, catch the exception:#import<Foundation/Foundation.h>@interfaceNsarray (Crash)@end/** *---------------Split Line---------------* **/ #import "nsarray+crash.h"#import<objc/runtime.h>@implementationNsarray (Crash)+ (void) load{[super load]; //Replace immutable group methodsMethod Oldobjectatindex = Class_getinstancemethod (Objc_getclass ("__nsarrayi"), @selector (objectatindex:)); Method Newobjectatindex= Class_getinstancemethod (Objc_getclass ("__nsarrayi"), @selector (objectatsafeindex:)); Method_exchangeimplementations (Oldobjectatindex, Newobjectatindex); //replacing variable array methodsMethod Oldmutableobjectatindex = Class_getinstancemethod (Objc_getclass ("__nsarraym"), @selector (objectatindex:)); Method Newmutableobjectatindex= Class_getinstancemethod (Objc_getclass ("__nsarraym"), @selector (mutableobjectatsafeindex:)); Method_exchangeimplementations (Oldmutableobjectatindex, Newmutableobjectatindex);}- (ID) Objectatsafeindex: (Nsuinteger) index{if(Index > Self.count-1|| !self.count) {@try { return[self objectatsafeindex:index]; } @catch(NSException *exception) {NSLog (@"Exception:%@", Exception.reason); returnNil; } }Else { return[self objectatsafeindex:index]; }}- (ID) Mutableobjectatsafeindex: (Nsuinteger) index{if(Index > Self.count-1|| !self.count) {@try { return[self mutableobjectatsafeindex:index]; } @catch(NSException *exception) {NSLog (@"Exception:%@", Exception.reason); returnNil; } }Else { return[self mutableobjectatsafeindex:index]; }}@end
http://blog.csdn.net/hero_wqb/article/details/78531218
17th Month 7th Day IOS array out of bounds, anti-crash processing