My NSMutableArray doesn't work

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

Answers (2)

Alex Martini
Alex Martini

Reputation: 760

It looks like you're missing a loop of some kind. The code you list above:

  • Makes sure something isn't paused.
  • Creates a new (empty) mutable array.
  • Adds a value to the new array.
  • And does some other work.

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

dreamlax
dreamlax

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

Related Questions