[英]ios: NSNull lldb error when using substringToIndex
我有以下代码:
NSString *subtitleFull = [dictionary valueForKey:@"distance"];
NSString *subtitle = [subtitleFull substringToIndex:2];
当我运行代码时,出现以下错误:
-[NSNull substringToIndex:]: unrecognized selector sent to instance 0x3a61a080
我检查了字典是否返回了NSString并完成了以下工作:
id idont = [dictionary valueForKey:@"distance"];
if ([idont isKindOfClass:[NSString class]]){
NSLog(@"*****************YEA THIS IS IT %@", idont);
}
字符串看起来像:“ 12.0093778471967”,然后变成NSString类型。 也许我需要使用int值方法?
我为什么会收到此错误?
错误消息非常简单, subtitleFull
为空。 因为通常如果不存在键,则返回值为零。 因此,在这种情况下,我想为什么字典中的值为null
就是因为dictionary
来自JSON字符串或XML。 只需在使用前检查该值是否为空即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.