Reputation: 1
sorry for my stupid question (beginner) I got the demo program Accelerometergraph the apple site and would like to use NSMutableArray in the values of acceleration x. but my NSMutableArray contains only one object, there being several passage NSMutableArray routine and should contain the same number of objects that the counter show, how code below
if(!isPaused)
{
array = [[NSMutableArray alloc] init];
[filter addAcceleration:acceleration];
[unfiltered addX:acceleration.x y:acceleration.y z:acceleration.z];
NSNumber *number = [[NSNumber alloc] initWithDouble:acceleration.x];
[array addObject:number];
++a;
if (a == 30) // only check the # objs of mutablearray
{
sleep(2);
}
[filtered addX:filter.x y:filter.y z:filter.z];
}
Upvotes: 0
Views: 211
Reputation: 760
It looks like you're missing a loop of some kind. The code you list above:
My guess is that this whole if{} block sits inside some kind of loop. You need to alloc and init the mutable array outside of the loop instead.
Upvotes: 3
Reputation: 95315
You create a new array each time the if
block is entered, so the addObject:
will only add the object to the most recently created array.
Furthermore, you are leaking the array
and number
objects. Each time you allocate an object, you are responsible for releasing it. Make sure you're familiar with the guidelines set out in the memory management programming guide.
Upvotes: 1