Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nutthick

macrumors newbie
Original poster
Apr 11, 2007
26
0
I'm having problems with pointers and NSMutableArray. When I add an object to an array it gets passed the address of the object. In my method I want to reuse the object, changing certain parameters each time, to build up an array of different objects, however it keeps passing the same address to the array so all objects end up the same. I was hoping using release would work, but no luck. How should I be writing this?

Thanks

Code:
MyObject *anObject = [[MyObject alloc] init];

[anObject setSomeParameter:1];
[AllObjects addObject:anObject];

[anObject release];
[anObject setSomeParameter:2];
[allObjects addObject:anObject];

[anObject release];
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Does MyObject have any factory methods like:
[MyObject objectWithParameter:2];

That returns a new MyObject initialized with the parameter given?

If so, this would be the way I'd deal with it. If you immediately place this into an NSMutableArray, this will cause it to be retained, so you don't need to yourself.

Otherwise you need to alloc and init a new object each time. If you're going to use the same pointer, that means you won't have access to the Object outside of the array after that point, so you should send it release before reusing the pointer. Or remembering to release each object in the array before it's removed, etc. which may be more difficult.

-Lee
 

nutthick

macrumors newbie
Original poster
Apr 11, 2007
26
0
Using alloc and init all the time looked clunky, so I was trying to avoid it.

MyObject is only a selection of floats and integers using property and sythesize. If I was to include an initializeWith would my code simply become?

Code:
MyObject *anObject = [MyObject alloc];
[allObjects addObject:[anObject initWithParameter:1];
[allObjects addObject:[anObject initWithParameter:2];

[anObject release];
Thanks for your help.
 

Krevnik

macrumors 601
Sep 8, 2003
4,101
1,312
Your method for setting up the objects just won't work that way. You only alloc one object, and the array is /not/ making copies for you. You are adding the same object twice (because you only have one object).

What you need to do is something more like this:

Code:
MyObject *anObject = [[MyObject alloc] initWithParameter:1];
[AllObjects addObject:anObject];
[anObject release];

anObject = [[MyObject alloc] initWithParameter:2];
[allObjects addObject:anObject];
[anObject release];
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Using alloc and init all the time looked clunky, so I was trying to avoid it.

MyObject is only a selection of floats and integers using property and sythesize. If I was to include an initializeWith would my code simply become?

Code:
MyObject *anObject = [MyObject alloc];
[allObjects addObject:[anObject initWithParameter:1]];
[allObjects addObject:[anObject initWithParameter:2]];

[anObject release];
Thanks for your help.

Erm, not quite. You would need a class method that returned a new instance initialized with a particular value. So you'd have a method with the signature:
Code:
+(MyObject *) objectWithParameter: (int) setupParam;

In this, you would allocate a new MyObject, and you would initialize it with setupParam, then return it. This object should have a retain count of 0 and have autorelease called on it. This is the convention for things that return new objects that don't have init or copy in their name. You could then call this multiple times, like:
Code:
NSMutableArray *allObjects = [[NSMutableArray alloc] init];
[allObjects addObject:[MyObject objectWithParameter:1];
[allObjects addObject:[MyObject objectWithParameter:2];
...
[allObjects release];

Alternately, you could:
Code:
MyObject *anObject = [[MyObject alloc] init];
NSMutableArray *allObjects = [[NSMutableArray alloc] init];
[anObject setParameter:1];
[allObjects addObject:anObject];
[anObject release];

MyObject *anObject = [[MyObject alloc] init];
[anObject setParameter:2];
[allObjects addObject:anObject];
[anObject release];
...
[allObjects release];

If you had an init method that took a parameter, you could use that in the place of init/setParameter.

-Lee

Edit: Balls, Krevnik beat me to it.
 

nutthick

macrumors newbie
Original poster
Apr 11, 2007
26
0
The class method looks perfect. I should be OK from here.

Thanks again
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.