【问题标题】:NSNumber: stringValue crashes, description worksNSNumber:stringValue 崩溃,描述有效
【发布时间】:2011-08-22 16:18:10
【问题描述】:

[measureValue stringValue] 给了我这个例外:

由于未捕获的异常“NSInvalidArgumentException”而终止应用,原因:“-[NSCFString stringValue]: unrecognized selector sent to instance 0x4dde570”

[measureValue description] 完美运行

但我认为我应该在代码中使用stringValue,对吧?

这是代码:

NSNumber *measureValue = [NSString stringWithFormat:@"%i", [cellContent integerValue]];

//if imperial (inches)
if ([[[NSUserDefaults standardUserDefaults] stringForKey:@"Measure"] isEqualToString:@"Imperial"] ) {

    //if height
    if ([currentQuestion isEqualToString:@"Height"]) {
        measureValue = [NSNumber numberWithDouble:(2.54 * [measureValue doubleValue])];
    //elseif weight
    } else {
        measureValue = [NSNumber numberWithDouble:(0.45359237 * [measureValue doubleValue])];
    }

    //NSLog(@"%@", [measureValue stringValue]);
    cellContent = [measureValue description];

【问题讨论】:

  • 它崩溃是因为 NSString 没有响应 stringValue。为什么将 measureValue 设置为字符串?将其设置为 NSNumber。
  • 你忽略了编译器关于指针类型不兼容的警告,为什么?

标签: objective-c cocoa exception nsstring nsnumber


【解决方案1】:
 NSNumber *measureValue = [NSString stringWithFormat:@"%i", [cellContent integerValue]];

该行返回一个 NSString,而不是一个 NSNumber。您正在将 stringValue 发送到 NSString。

【讨论】:

    猜你喜欢
    • 2018-04-21
    • 1970-01-01
    • 1970-01-01
    • 2018-09-06
    • 1970-01-01
    • 1970-01-01
    • 2013-09-17
    • 1970-01-01
    • 2016-01-12
    相关资源
    最近更新 更多