Creating an array of custom objects in Objective C

13,780

Solution 1

Your for loop code is correct with the exception of deallocating myPerson at the end of the loop. You do not want to deallocate it since the object is retained by the array. Generally you do not want to invoke dealloc directly, you want to use retain/release instead. For more information about memory management check out Apple's Guide

So you are correct with the exception of dealloc being changed to release.

for (int i = 0; i < 20; i++)
{
    Person *myPerson = [[Person alloc] init];
    myPerson.name = @"Brian";
    myPerson.age = [NSNumber numberWithInteger:23];     
    [myArray addObject:myPerson];       
    [myPerson release];         
}

Solution 2

Probably the simplest and most "obj-c" way to do this would be to create a class method on person that creates the person you want. Just like [NSString stringWithFormat], you'd return an autoreleased object so that the caller doesn't even need to release it.

Never call dealloc yourself - trust in release

- (Person*) personWithName:(NSString*)name andAge:(NSNumber*)age
{
     Person* p = [[Person alloc] init];
     p.name = name;
     p.age = age;
     [p autoRelease];
     return p;
}

Now you'll be able to add each person to the array in a single line without leaking memory

[myArray addObject:[Person personWithName:@"Brian" andAge:23]];
[myArray addObject:[Person personWithName:@"Alice" andAge:4]];
[myArray addObject:[Person personWithName:@"Bob" andAge:60]];  

Further reading: http://www.cocoadev.com/index.pl?AutoRelease

Solution 3

As DHamrick writes you should not dealloc the object you create, this is the same as deleting the object. It was added to the array, which also retained it. But as you in the loop creates a new object I can imagine the new object will have the same address as the previously deleted one and thus the old pointers will again point at this new object, making you believe they are all one and the same. To hunt down bugs like this I suggest you look into enabling NSZombie's.

http://iosdevelopertips.com/debugging/tracking-down-exc_bad_access-errors-with-nszombieenabled.html

Share:
13,780
Admin
Author by

Admin

Updated on June 12, 2022

Comments

  • Admin
    Admin almost 2 years

    EDIT What i am trying to achieve with the code, is to create an array of 20 unique 'Person' objects. I know now thanks to the guys below that i am going way off calling dealloc.

    for (int i = 0; i < 20; i++)
    {
        Person *myPerson = [[Person alloc] init];
        myPerson.name = @"Brian";
        myPerson.age = [NSNumber numberWithInteger:23];     
        [myArray addObject:myPerson];       
        [myPerson dealloc];         
    }
    

    In the process of learning Objective C, hence why this code might seem crazy to you. But I am coming from Java so my logic isn't great for Objective C yet.

    I was trying to populate my array with 20 person objects. But it seems i am adding the same Object person all the time. Then deallocating it which defeats the point.

    So my question is: What would be the correct way to to fill the array with 20 different person objects. The names and ages will be different in the future for each person but at the moment they all be the same while i learn.

    Is the only way to do this to type a lot of code like this :

        Person *myPerson1 = [[Person alloc] init];
        myPerson.name = @"Brian";
        myPerson.age = [NSNumber numberWithInteger:23];     
        [myArray addObject:myPerson1];  
        Person *myPerson2 = [[Person alloc] init];
        myPerson.name = @"Brian";
        myPerson.age = [NSNumber numberWithInteger:23];     
        [myArray addObject:myPerson2];  
        Person *myPerson3 = [[Person alloc] init];
        myPerson.name = @"Brian";
        myPerson.age = [NSNumber numberWithInteger:23];     
        [myArray addObject:myPerson3];  
    

    20 times? Or is there a more elegant solution?

    Thanks for you advice. -Code