views:

55

answers:

2

hello guys sorry for my stupid question (beginner) I got the demo program Accelerometergraph the apple site and would like to use mutablearray in the values of acceleration x. but my mutablearray contains only one object, there being several passage mutablearray 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];
}
+3  A: 

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.

Alex Martini
+1  A: 

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.

dreamlax