views:

355

answers:

3

I am getting tricked up with arrays and nested arrays.

I would like this if statement to evaluate to True if lastObject doesn'texist, or if accelerometer.x - lastObject[0] > .1

My code doesn't do this.

if (![accelerometerArray lastObject] || 
  [NSNumber numberWithDouble:acceleration.x] - 
  [[accelerometerArray lastObject] objectAtIndex: 0] > [NSNumber numberWithDouble:.1) 
 {

  NSArray *entry = [[NSArray alloc] initWithObjects:
                    [NSNumber numberWithDouble:acceleration.x],
                    [NSNumber numberWithDouble:acceleration.y],
                    [NSNumber numberWithDouble:acceleration.z], 
                    [NSDate date]];

  [accelerometerArray addObject: entry];
  [entry release];
}
+1  A: 

you got an extra ":" in between [accelerometerArray lastObject] and objectAtIndex:0

this line

[[accelerometerArray lastObject]: objectAtIndex: 0]

Zteeth
Thanks, now I'm getting a new error :) "error: invalid operands to binary - (have 'struct NSNumber' and 'id')"
Andrew Johnson
+1  A: 

you're missing a closing "]" on "[NSNumber numberWithDouble:.1" and i think you may need to write it like:

[[NSNumber numberWithDouble:acceleration.x] doubleValue] - [[[accelerometerArray lastObject] objectAtIndex: 0] doubleValue] > 0.1)

i think you can swap out "[NSNumber numberWithDouble:.1]" with 0.1 too.

taber
A: 

You also need to end your NSArray objects with nil or bad things will happen. nil denotes that it is the end of the array and traversal can stop.

NSArray *entry = [[NSArray alloc] initWithObjects: [NSNumber numberWithDouble:acceleration.x], [NSNumber numberWithDouble:acceleration.y], [NSNumber numberWithDouble:acceleration.z], [NSDate date], nil];

Scott Densmore