Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 由于未捕获异常而终止应用程序';nsrange异常';,原因:';***-[\uu NSArrayM objectAtIndex:]:索引0超出空数组的界限_Ios_Objective C_Uitableview_Nsrangeexception - Fatal编程技术网

Ios 由于未捕获异常而终止应用程序';nsrange异常';,原因:';***-[\uu NSArrayM objectAtIndex:]:索引0超出空数组的界限

Ios 由于未捕获异常而终止应用程序';nsrange异常';,原因:';***-[\uu NSArrayM objectAtIndex:]:索引0超出空数组的界限,ios,objective-c,uitableview,nsrangeexception,Ios,Objective C,Uitableview,Nsrangeexception,因此,我有一个UITableView,每当阵列中没有任何项目时,应用程序就会因此错误而崩溃。奇怪的是,我有另一个表,当它在数组中没有对象时,它不会崩溃,并且我对每个表使用几乎相同的代码。以下是我所拥有的: @implementation FindFBFriendsViewController { NSMutableArray *friends; } - (void)viewDidLoad { [super viewDidLoad]; // Do any addition

因此,我有一个UITableView,每当阵列中没有任何项目时,应用程序就会因此错误而崩溃。奇怪的是,我有另一个表,当它在数组中没有对象时,它不会崩溃,并且我对每个表使用几乎相同的代码。以下是我所拥有的:

@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;
}
以下是堆栈:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' *** First throw call stack: ( 0 CoreFoundation 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 QuartzCore 0x000000010974908e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366 13 QuartzCore 0x0000000109748f0c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 14 QuartzCore 0x000000010973d3c9 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277 15 QuartzCore 0x000000010976b086 _ZN2CA11Transaction6commitEv + 486 16 UIKit 0x000000010407b72e _UIApplicationHandleEventQueue + 7135 17 CoreFoundation 0x0000000106139301 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 18 CoreFoundation 0x000000010612f22c __CFRunLoopDoSources0 + 556 19 CoreFoundation 0x000000010612e6e3 __CFRunLoopRun + 867 20 CoreFoundation 0x000000010612e0f8 CFRunLoopRunSpecific + 488 21 GraphicsServices 0x0000000109204ad2 GSEventRunModal + 161 22 UIKit 0x0000000104080f09 UIApplicationMain + 171 23 gif-wrap 0x00000001033922ef main + 111 24 libdyld.dylib 0x000000010752892d start + 1 25 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException 由于未捕获异常“NSRangeException”而终止应用程序,原因:“***-[\uu NSArrayM objectAtIndex:]:索引0超出空数组的界限” ***第一次抛出调用堆栈: ( 0 CoreFoundation 0x0000000106213d85例外预处理+165 1 libobjc.A.dylib 0x000000010597adeb objc_异常_抛出+48 2核心基金会0x00000001060f1804-[\uu NSArrayM objectAtIndex:][212 3 gif包裹0x000000010338dd24-[FindFBFriendsViewController viewDidLoad]+340 4 UIKit 0x000000010421b984-[UIViewController loadViewIfRequired]+1198 5 UIKit 0x000000010422193b-[UIViewController\uuuu视图将出现:][120 6 UIKit 0x0000000104251750-[UINavigationController\u startCustomTransition:+1203 7 UIKit 0x0000000104261b9b-[UINAVIGATIONCONTROLLENT\u STARTDEFERREEDTRANSION(如果需要):+712 8 UIKit 0x0000000104262d0b-[UINavigationController\uuu视图将布局子视图]+57 9 UIKit 0x0000000104411503-[UILayoutContainerView布局子视图]+248 10 UIKit 0x000000010413b980-[UIView(CALayerDelegate)布局层的子层:+703 11夸脱核心0x0000000109754c00-[CALayer布局子层]+146 12夸脱核心0x000000010974908e锌层布局(如果需要)PNS 11交易+366 13 QuartzCore 0x0000000109748f0c\u Zn2CA58层布局和显示(如果需要)PNS\u 11事务+24 14 QuartzCore 0x000000010973d3c9_ZN2CA7Context18提交交易PNS_11交易+277 15夸脱矿0x000000010976b086_Zn2Ca11交易承诺+486 16 UIKit 0x000000010407b72e _UIApplicationHandleEventQueue+7135 17 CoreFoundation 0x0000000106139301 \uuuu CFRUNLOOP\u正在调用\uu OUT\uu以执行\uu函数\uuuu+17 18 CoreFoundation 0x000000010612f22c\uU CFRunLoopDoSources0+556 19 CoreFoundation 0x000000010612e6e3\uuu CFRunLoopRun+867 20 CoreFoundation 0x000000010612e0f8 CFRunLoopRunSpecific+488 21图形服务0x0000000109204ad2 GSEventRunModal+161 22 UIKit 0x0000000104080f09 UIApplicationMain+171 23 gif环绕0x00000001033922ef main+111 24 libdyld.dylib 0x000000010752892d开始+1 25°?0x0000000000000001 0x0+1 ) libc++abi.dylib:以NSException类型的未捕获异常终止
有人知道我如何解决这个问题吗?

您自己说,当friends数组为空时,就会发生这种情况。运行以下操作时,您希望看到什么:

IndFBFriend *testFriend = [friends objectAtIndex:0];
);当
friends
为空时,这就是您看到的。索引0对于空数组无效


您尚未发布其他代码,但我猜您的其他代码不包含这样的行,因此不会崩溃。

您自己说,当friends数组为空时,会发生这种情况。运行以下操作时,您希望看到什么:

IndFBFriend *testFriend = [friends objectAtIndex:0];
);当
friends
为空时,这就是您看到的。索引0对于空数组无效


您尚未发布其他代码,但我猜您的其他代码不包含这样的行,因此不会崩溃。

使用
indfbffriend*testFriend=[friends firstObject]哪个更安全。

使用
IndFBFriend*testFriend=[friends firstObject]哪个更安全。

谢谢你指出这一点。我在测试之前添加了NSLog语句,但完全忘记了将其取出。通常,如果您看到异常,请查看相关消息,因为它通常很有用。在本例中,您已经知道这是在访问空数组时发生的,但有两个提示供您参考:
-[\uu nsarray-objectAtIndex:::
表示崩溃发生在
-objectAtIndex:
处(因此,请自己审核调用
-objectAtIndex:
的位置),但更重要的是,如果查看回溯,可以看到崩溃是由
-viewDidLoad
中的
-objectAtIndex:
引起的(调用堆栈第2行和第3行)。这里只有一个,所以这就是问题所在。哈,谢谢你指出这一点。我在测试之前添加了NSLog语句,但完全忘记了将其取出。通常,如果您看到异常,请查看相关消息,因为它通常很有用。在本例中,您已经知道这是在访问空数组时发生的,但有两个提示供您参考:
-[\uu nsarray-objectAtIndex:::
表示崩溃发生在
-objectAtIndex:
处(因此,请自己审核调用
-objectAtIndex:
的位置),但更重要的是,如果查看回溯,可以看到崩溃是由
-viewDidLoad
中的
-objectAtIndex:
引起的(调用堆栈第2行和第3行)。那里只有一个,所以这就是问题所在。