您的位置:首页 > 产品设计 > UI/UE

UITableView 避免奔溃的数组越界处理方法

2015-05-09 12:05 681 查看
0x7403d 0x3b36cab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
这个方法objectAtIndex:的说明
- (id)objectAtIndex:(NSUInteger)index
Description
Returns the object located at the specified index.
If index is beyond the end of the array (that is, if index is greater than or equal to the value returned by count),
an NSRangeException is raised.超出了界限就会抛出异常
Parameters
index
An index within the bounds of the array.
我们可以写一个类目来避免数组越界后直接崩溃的情形(或许崩溃是最好结果,但我们有时候可以直接根据判断数组取值为nil避免崩溃),代码如下:
////NSArray+YXInfo.h//BeyondTheMark////Copyright (c) 2014年 Y.X. All rights reserved.//#import <Foundation/Foundation.h>@interface NSArray (YXInfo)- (id)objectAt:(NSUInteger)index;@end
////NSArray+YXInfo.m//BeyondTheMark////Copyright (c) 2014年 Y.X. All rights reserved.//#import "NSArray+YXInfo.h"@implementation NSArray (YXInfo)- (id)objectAt:(NSUInteger)index{	if (index < self.count)	{		return self[index];	}	else	{		return nil;	}}@end

实现原理超级简单呢:)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: