【发布时间】: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