由于未捕获的异常 'NSRangeException' 而终止应用程序,原因:'*** -[__NSArrayM objectAtIndex:]:索引 0 超出空数组的范围
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array
所以我有一个 UITableView,每当数组中没有任何项目时,应用程序就会崩溃并出现此错误。奇怪的是,我有另一个 table 当它在数组中有零个对象时不会崩溃,而且我为每个对象使用几乎相同的代码。这是我拥有的:
@implementation FindFBFriendsViewController {
NSMutableArray *friends;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UserFBFriends *fbFriends = [UserFBFriends sharedInstance];
NSLog(@"Hell0 from the fbfriends view, array is %@", fbFriends.userFriends);
friends = [[NSMutableArray alloc] initWithArray:fbFriends.userFriends];
IndFBFriend *testFriend = [friends objectAtIndex:0];
NSLog(@"USER Friends: %@", testFriend.fullName);
}
-(void)viewWillAppear:(BOOL)animated {
[self.fbTableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [friends count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
FBFriendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"friendCell" forIndexPath:indexPath];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FBFriendCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
IndFBFriend *indexRowFriend = [friends objectAtIndex:indexPath.row];
[cell updateCellWithName:indexRowFriend.fullName profilePicture:indexRowFriend.profPic personFBId:indexRowFriend.fbId];
cell.backgroundColor = [UIColor clearColor];
return cell;
}
这是堆栈:
由于未捕获的异常 'NSRangeException' 而终止应用程序,原因:'*** -[__NSArrayM objectAtIndex:]:索引 0 超出空数组的范围'
*** 首先抛出调用栈:
(
0 核心基础 0x0000000106213d85 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010597adeb objc_exception_throw + 48
2 CoreFoundation 0x00000001060f1804 -[__NSArrayM objectAtIndex:] + 212
3 gif-wrap 0x000000010338dd24 -[FindFBFriendsViewController viewDidLoad] + 340
4 UIKit 0x000000010421b984 -[UIViewController loadViewIfRequired] + 1198
5 UIKit 0x000000010422193b -[UIViewController __viewWillAppear:] + 120
6 UIKit 0x0000000104251750 -[UINavigationController _startCustomTransition:] + 1203
7 UIKit 0x0000000104261b9b -[UINavigationController _startDeferredTransitionIfNeeded:] + 712
8 UIKit 0x0000000104262d0b -[UINavigationController __viewWillLayoutSubviews] + 57
9 UIKit 0x0000000104411503 -[UILayoutContainerView layoutSubviews] + 248
10 UIKit 0x000000010413b980 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703
11 QuartzCore 0x0000000109754c00 -[CALayer layoutSublayers] + 146
12 石英核 0x000000010974908e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
13 石英核 0x0000000109748f0c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
14 石英核 0x000000010973d3c9 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
15 石英核心 0x000000010976b086 _ZN2CA11Transaction6commitEv + 486
16 UIKit 0x000000010407b72e _UIApplicationHandleEventQueue + 7135
17 核心基础 0x0000000106139301 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
18 核心基础 0x000000010612f22c __CFRunLoopDoSources0 + 556
19 核心基础 0x000000010612e6e3 __CFRunLoopRun + 867
20 核心基础 0x000000010612e0f8 CFRunLoopRunSpecific + 488
21 图形服务 0x0000000109204ad2 GSEventRunModal + 161
22 UIKit 0x0000000104080f09 UIApplicationMain + 171
23 gif-wrap 0x00000001033922ef main + 111
24 libdyld.dylib 0x000000010752892d 开始 + 1
25 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib:以 NSException 类型的未捕获异常终止
有人知道我该如何解决这个问题吗?
你自己说当 friends 数组为空时会发生这种情况。当您 运行 以下内容时,您希望看到什么:
IndFBFriend *testFriend = [friends objectAtIndex:0];
-[NSArray objectAtIndex:]
throws an exception when you access an index that does not exist;当 friends
为空时,这就是您所看到的。索引 0 对于空数组无效。
你还没有发布你的其他代码,但我猜你的其他代码不包含这样一行,所以它不会崩溃。
使用更安全的IndFBFriend *testFriend = [friends firstObject];
。
所以我有一个 UITableView,每当数组中没有任何项目时,应用程序就会崩溃并出现此错误。奇怪的是,我有另一个 table 当它在数组中有零个对象时不会崩溃,而且我为每个对象使用几乎相同的代码。这是我拥有的:
@implementation FindFBFriendsViewController {
NSMutableArray *friends;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UserFBFriends *fbFriends = [UserFBFriends sharedInstance];
NSLog(@"Hell0 from the fbfriends view, array is %@", fbFriends.userFriends);
friends = [[NSMutableArray alloc] initWithArray:fbFriends.userFriends];
IndFBFriend *testFriend = [friends objectAtIndex:0];
NSLog(@"USER Friends: %@", testFriend.fullName);
}
-(void)viewWillAppear:(BOOL)animated {
[self.fbTableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [friends count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
FBFriendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"friendCell" forIndexPath:indexPath];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FBFriendCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
IndFBFriend *indexRowFriend = [friends objectAtIndex:indexPath.row];
[cell updateCellWithName:indexRowFriend.fullName profilePicture:indexRowFriend.profPic personFBId:indexRowFriend.fbId];
cell.backgroundColor = [UIColor clearColor];
return cell;
}
这是堆栈:
由于未捕获的异常 'NSRangeException' 而终止应用程序,原因:'*** -[__NSArrayM objectAtIndex:]:索引 0 超出空数组的范围' *** 首先抛出调用栈: ( 0 核心基础 0x0000000106213d85 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x000000010597adeb objc_exception_throw + 48 2 CoreFoundation 0x00000001060f1804 -[__NSArrayM objectAtIndex:] + 212 3 gif-wrap 0x000000010338dd24 -[FindFBFriendsViewController viewDidLoad] + 340 4 UIKit 0x000000010421b984 -[UIViewController loadViewIfRequired] + 1198 5 UIKit 0x000000010422193b -[UIViewController __viewWillAppear:] + 120 6 UIKit 0x0000000104251750 -[UINavigationController _startCustomTransition:] + 1203 7 UIKit 0x0000000104261b9b -[UINavigationController _startDeferredTransitionIfNeeded:] + 712 8 UIKit 0x0000000104262d0b -[UINavigationController __viewWillLayoutSubviews] + 57 9 UIKit 0x0000000104411503 -[UILayoutContainerView layoutSubviews] + 248 10 UIKit 0x000000010413b980 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703 11 QuartzCore 0x0000000109754c00 -[CALayer layoutSublayers] + 146 12 石英核 0x000000010974908e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366 13 石英核 0x0000000109748f0c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 14 石英核 0x000000010973d3c9 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277 15 石英核心 0x000000010976b086 _ZN2CA11Transaction6commitEv + 486 16 UIKit 0x000000010407b72e _UIApplicationHandleEventQueue + 7135 17 核心基础 0x0000000106139301 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 18 核心基础 0x000000010612f22c __CFRunLoopDoSources0 + 556 19 核心基础 0x000000010612e6e3 __CFRunLoopRun + 867 20 核心基础 0x000000010612e0f8 CFRunLoopRunSpecific + 488 21 图形服务 0x0000000109204ad2 GSEventRunModal + 161 22 UIKit 0x0000000104080f09 UIApplicationMain + 171 23 gif-wrap 0x00000001033922ef main + 111 24 libdyld.dylib 0x000000010752892d 开始 + 1 25 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib:以 NSException 类型的未捕获异常终止
有人知道我该如何解决这个问题吗?
你自己说当 friends 数组为空时会发生这种情况。当您 运行 以下内容时,您希望看到什么:
IndFBFriend *testFriend = [friends objectAtIndex:0];
-[NSArray objectAtIndex:]
throws an exception when you access an index that does not exist;当 friends
为空时,这就是您所看到的。索引 0 对于空数组无效。
你还没有发布你的其他代码,但我猜你的其他代码不包含这样一行,所以它不会崩溃。
使用更安全的IndFBFriend *testFriend = [friends firstObject];
。