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//BeyondTheMarkCopyright (c) 2014年 Y.X. All rights reserved.//#import <Foundation/Foundation.h>@interface NSArray (YXInfo)- (id)objectAt:(NSUInteger)index;@end NSArray+YXInfo.m//BeyondTheMarkCopyright (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 实现原理超级简单呢:)
UITableView 避免奔溃的数组越界处理方法
最新推荐文章于 2021-05-08 20:43:24 发布