1

I am trying to add two objects to my array but when i inspect the array, the second object appears in the list twice. Does anyone know why?

list = [[NSMutableArray alloc] init];
Person *person = [[Person alloc] init];

// Create person 1
person.name = @"Fred";
person.gender = @"unknown";

// Append to array
[list addObject:person];
[person release];

// Create person 2
person.name = @"Bob";
person.gender = @"male";

// Append to array again
[list addObject:person];
[person release];

3 Answers 3

3

You're not creating two person instances, but just one.

Either do this:

list = [[NSMutableArray alloc] init];
Person *person;

// Create person 1
person = [[Person alloc] init];
person.name = @"Fred";
person.gender = @"unknown";

// Append to array
[list addObject:person];
[person release];

// Create person 2
person = [[Person alloc] init]; //this line right here!
person.name = @"Bob";
person.gender = @"male";

// Append to array again
[list addObject:person];
[person release];

or assign a second person to a second variable.

The person variable still points to the first person instance. You need to change the variable to a new instance, otherwise you're simply inserting the person twice and also overwriting its properties.

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

3 Comments

I originally did this, but you get the error Redefinition of 'person'
You probably forgot to remove the Person * for the second line.
The Person * part is the definition. The person = …; part is the assignment. You can either combine them into one expression or split them up (as I did. Not necessary though, just clearer, imho).
3

The problem is that the array does not make a copy of the object you passed to it to add, instead it only holds a reference to the object. So what you're doing is creating an object, assigning to its properties, giving a reference to the array, mutating the same object, and then adding the same (but mutated) object to the array again.

You need to make a new Person object each time instead of mutating the same object.

Comments

2

You've only created one person. Try this:

list = [[NSMutableArray alloc] init];
Person *person = [[Person alloc] init];

// Create person 1
person.name = @"Fred";
person.gender = @"unknown";

// Append to array
[list addObject:person];
[person release];

// Create person 2
Person *person2 = [[Person alloc] init];
person2.name = @"Bob";
person2.gender = @"male";

// Append to array again
[list addObject:person2];
[person release];

The issue here is that when you added the first person to the array, and then modified the original object, the object is also modified in the array --- you need to instantiate a new version of the "person" object and modify it.

If you want to create many, many people, I suggest using a for loop:

NSArray *names = [NSArray arrayWithObjects:@"Fred", @"Bob"];
NSArray *genders = [NSArray arrayWithObjects:@"unknown", @"male"];

for (int i = 0; i<[names count]; i++) {
    Person *person = [[Person alloc] init];
    person.name = [names objectAtIndex:i];
    person.gender = [genders objectAtIndex:i];
    [list addObject:person];
    [person release];
}

1 Comment

This works, but how would you re-use (re-initialise) the person variable so you don't have to create different Person variables?

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.