PDA

View Full Version : NSMutableArray and Pointers




nutthick
Sep 10, 2008, 03:16 PM
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

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

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

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

[anObject release];



lee1210
Sep 10, 2008, 03:56 PM
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
Sep 10, 2008, 04:53 PM
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?

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

[anObject release];

Thanks for your help.

Krevnik
Sep 10, 2008, 05:03 PM
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:


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

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

lee1210
Sep 10, 2008, 05:09 PM
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?

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:
+(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:

NSMutableArray *allObjects = [[NSMutableArray alloc] init];
[allObjects addObject:[MyObject objectWithParameter:1];
[allObjects addObject:[MyObject objectWithParameter:2];
...
[allObjects release];


Alternately, you could:

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
Sep 10, 2008, 05:12 PM
The class method looks perfect. I should be OK from here.

Thanks again