0

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'

1 Answer 1

2

The following line is causing the error

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

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.