0

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];
}
Code cracker
3,1666 gold badges42 silver badges76 bronze badges
asked Aug 4, 2010 at 18:42
3
  • Please format your code correctly if you want help. Commented Aug 4, 2010 at 18:45
  • Your NSNumber initialization can be simplified by doing the following, I believe: NSNumber *number = [NSNumber numberWithDouble:acceleration.x]; Commented Aug 4, 2010 at 19:05
  • Also, because you never release the NSNumber object you create, you're leaking memory. Commented Aug 8, 2010 at 15:01

2 Answers 2

3

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.

Joshua Nozzi
61.3k14 gold badges142 silver badges136 bronze badges
answered Aug 4, 2010 at 18:46
Sign up to request clarification or add additional context in comments.

Comments

1

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.

answered Aug 4, 2010 at 21:15

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.