2016-11-25 94 views
0

因此,我有一個UITableView,並且每當該應用程序崩潰時數組中沒有任何項目。奇怪的是,我有另一個表,當它在數組中沒有對象時不會崩潰,並且我爲每個表使用幾乎相同的代碼。這裏是我有:由於未捕獲的異常'NSRangeException',原因:'*** - [__ NSArrayM objectAtIndex:]:index 0超出空數組界限的應用程序

@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 

是否有人知道如何解決這個問題呢?

回答

1

你說你自己,當朋友數組爲空時會發生這種情況。當您執行以下操作時,您會看到什麼結果:

IndFBFriend *testFriend = [friends objectAtIndex:0]; 

-[NSArray objectAtIndex:] throws an exception when you access an index that does not exist;當friends爲空時,這就是你所看到的。索引0對於空數組無效。

你還沒有發佈你的其他代碼,但我猜你的其他代碼不包含這樣的行,所以它不會崩潰。

+0

哈感謝您指出。我補充說,先前測試的NSLog語句,完全忘了把它拿出來。 – hermt2

+0

一般來說,如果您看到異常,請查看關聯的消息,因爲它通常很有幫助。在這種情況下,您已經知道這是在訪問空數組期間發生的,但有兩個提示可供您查看:' - [__ NSArrayM objectAtIndex:]:'表示崩潰發生在'-objectAtIndex:' (所以在你自己調用'-objectAtIndex:'的地方進行審計),但更重要的是,如果你看看回溯,你會發現崩潰是由於你的'-viewDidLoad'中的'-objectAtIndex:'(調用堆棧行2和3)。這裏只有一個,所以這是問題。 –

1

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

相關問題