17th Month 7th Day IOS array out of bounds, anti-crash processing

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.