【问题标题】:NSNumber doubleValue method returns null in objective C (IOS)NSNumber doubleValue 方法在目标 C (IOS) 中返回 null
【发布时间】:2012-04-11 22:02:36
【问题描述】:

我正在做一些 iOS 学习,我有一个 NSNumber,它似乎是有序的,但是当我要求它的 doubleValue 时,我得到 null。

[stack addObject: [NSNumber numberWithDouble:operand]]
id topOfStack = [stack lastObject];
if([topOfStack isKindOfClass:[NSNumber class]]){
    NSLog(@"%@",topOfStack);
    NSLog(@"%@",[topOfStack class]);
    result  = [topOfStack doubleValue];
    NSLog(@"%@",result);
}

打印出来:

2012-04-11 17:52:04.909 RPNCalc[4127:f803] 3
2012-04-11 17:52:04.910 RPNCalc[4127:f803] __NSCFNumber
2012-04-11 17:52:04.910 RPNCalc[4127:f803] (null)

为什么 doubleValue 会显示为 null?

【问题讨论】:

    标签: objective-c ios nsnumber


    【解决方案1】:

    你得到 NULL 因为 %@ 是一个对象的字符串文字。双打不是对象。使用 %f NSLog 一个浮点数或双精度数。

    【讨论】:

      【解决方案2】:

      替换 NSLog(@"%@",topOfStack);与 NSLog(@"%g",topOfStack);

      【讨论】:

      • @CodaFi Apple 的 NSString 格式说明符与 IEEE 的 printf 匹配,因此 %f、%e、%E、%g 和 %G 都可以工作(但打印略有不同)
      • 我想你的意思是NSLog(@"%@",result)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-23
      • 2020-10-31
      • 2016-01-21
      • 1970-01-01
      相关资源
      最近更新 更多