如何自己做加盟网站,全国住房与城乡建设部网站,Crystal wordpress,linode 搭建wordpress在iOS开发中有时会遇到数组越界的问题#xff0c;从而导致程序崩溃。为了防止程序崩溃#xff0c;我们就要对数组越界进行处理。通过上网查资料#xff0c;发现可以通过为数组写一个分类来解决此问题。基本思路#xff1a;为NSArray写一个防止数组越界的分类。分类中利用ru…在iOS开发中有时会遇到数组越界的问题从而导致程序崩溃。为了防止程序崩溃我们就要对数组越界进行处理。通过上网查资料发现可以通过为数组写一个分类来解决此问题。基本思路为NSArray写一个防止数组越界的分类。分类中利用runtime将系统中NSArray的对象方法objectAtIndex:替换然后对objectAtIndex:传递过来的下标进行判断如果发生数组越界就返回nil,如果没有发生越界就继续调用系统的objectAtIndex:方法。代码.h文件#import #import interface NSArray (beyond)end.m文件#import NSArraybeyond.himplementation NSArray (beyond) (void)load{[superload];// 替换不可变数组中的方法Method oldObjectAtIndex class_getInstanceMethod(objc_getClass(__NSArrayI),selector(objectAtIndex:));Method newObjectAtIndex class_getInstanceMethod(objc_getClass(__NSArrayI),selector(__nickyTsui__objectAtIndex:));method_exchangeImplementations(oldObjectAtIndex, newObjectAtIndex);// 替换可变数组中的方法Method oldMutableObjectAtIndex class_getInstanceMethod(objc_getClass(__NSArrayM),selector(objectAtIndex:));Method newMutableObjectAtIndex class_getInstanceMethod(objc_getClass(__NSArrayM),selector(mutableObjectAtIndex:));method_exchangeImplementations(oldMutableObjectAtIndex, newMutableObjectAtIndex);}- (id)__nickyTsui__objectAtIndex:(NSUInteger)index{if (index self.count -1 || !self.count){try {return [self__nickyTsui__objectAtIndex:index];} catch (NSException *exception) {//__throwOutException 抛出异常NSLog(数组越界...);returnnil;} finally {}}else{return [self__nickyTsui__objectAtIndex:index];}}- (id)mutableObjectAtIndex:(NSUInteger)index{if (index self.count -1 || !self.count){try {return [selfmutableObjectAtIndex:index];} catch (NSException *exception) {//__throwOutException 抛出异常NSLog(数组越界...);returnnil;} finally {}}else{return [selfmutableObjectAtIndex:index];}}