I have a problem, storing instances of a viewController object. I want every user to have his/her own screen with some info on it and then be able to do modal to switch user.
The class instance of “User” and the array “Users” are defined in the .h file and the loop runs 5 times – but the array never gets populated:
- (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]); }
The user-object are created, but as mentioned, the array “users” never gets populated.
When it has run 5 times, it throws this error:
Terminating app due to uncaught exception
‘NSInvalidArgumentException’, reason: ‘* -[__NSArrayM
insertObject:atIndex:]: object cannot be nil’
The following line is causing the error
As the exception states, you can’t add nil to a NSArray. If you ever need to add a null value to a Cocoa collection, NSNull is the way to do it.