views:

27

answers:

2

Hi, Im trying to create an NSArray of floats. However, once the value is added to the arrays it is alway 0.0000000.

What am i doing wrong?

NSLog(@"percent: %f" , percent); prints the correct value and

NSLog(@"timeArray: %f", [timeArray objectAtIndex:i]); is always 0.00000.

 NSMutableArray *timeArray = [[NSMutableArray alloc] initWithCapacity:count];

 for (int i = 0; i < count; i++) {
  int time = Counter->getTime(i);

  float percent = (float)(time - startTime) / (float)endTime;
        NSLog(@"percent: %f" , percent); // This prints correct value

  [timeArray addObject:[NSNumber numberWithFloat: percent]];
  NSLog(@"timeArray: %f", [timeArray objectAtIndex:i]);  // This prints 0.00000
 }

Im on the iphone and running out of memory, could that be causing it?

+4  A: 

try

NSLog(@"timeArray: %f", [[timeArray objectAtIndex:i] floatValue]);

at the end.

You're trying to print out the pointer to the NSNumber, not the value stored in it.

Sam Dufel
Thanks, appreciate the help.
+4  A: 

in the second NSLog, you are printing an NSNumber, not the float inside..to see that value..

NSLog(@"timeArray: %f", [[timeArray objectAtIndex:i] floatValue]);
Jesse Naugher
For posterity, `NSLog(@"timeArray: %@", [timeArray objectAtIndex:i])` would also work.
Steven Fisher