2012-06-13 75 views
0

我有一個問題,存儲viewController對象的實例。我希望每個用戶都擁有自己的屏幕,並在屏幕上顯示一些信息,然後才能切換用戶模式。對象不存儲在NSMutableArray

「用戶」和陣列「用戶」的類的實例中的h文件中定義和循環運行5次 - 但從來沒有被填充的陣列:

- (void) chooseNumberOfUsers:(id)sender { 
    numberOfUsers = [sender tag]; 
    NSLog(@"Number of users: %i", numberOfUsers); 

    currentUser = 0; // Nul-indekseret 

    // Herefter skal vi oprette spillerobjekter 
    users = [[NSMutableArray alloc] init]; 
    for (int i=0; i<numberOfUsers; i++) { 
     user = [[UserViewController alloc] init]; 
     user.userid = i+1; 
     [users addObject:user]; 
    } 
    [users addObject:nil]; // Is this necessary? 

    // Debug: show info about the first user 
    NSLog(@"%@", [users objectAtIndex:0]); } 

用戶對象是創建,但如前所述,數組「用戶」永遠不會被填充。

當它運行5次,它拋出這個錯誤:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

回答

2

下面的行導致錯誤

[users addObject:nil]; // Is this necessary? 

由於異常狀態,你不能添加零到一NSArray的。如果您需要爲Cocoa集合添加空值,則可以使用NSNull

+0

非常感謝:) – Anders