【问题标题】:NSArry of floats issue浮动问题的 NSArry
【发布时间】:2010-10-26 15:45:05
【问题描述】:

我正在尝试创建一个 NSArray 的浮点数。但是,一旦将值添加到数组中,它始终为 0.0000000。

我做错了什么?

NSLog(@"percent: %f" , percent);打印正确的值并

NSLog(@"timeArray: %f", [timeArray objectAtIndex:i]);始终为 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
 }

我在iphone上内存不足,会不会是这个原因?

【问题讨论】:

    标签: iphone memory floating-point nsarray nsnumber


    【解决方案1】:

    在第二个 NSLog 中,您正在打印一个 NSNumber,而不是里面的浮点数..查看该值..

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

    【讨论】:

    • 对于后代,NSLog(@"timeArray: %@", [timeArray objectAtIndex:i]) 也可以。
    【解决方案2】:

    试试

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

    在最后。

    您正试图打印出指向 NSNumber 的指针,而不是其中存储的值。

    【讨论】:

      猜你喜欢
      • 2011-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多