尝试显示标签时,我收到[NSNull rangeOfCharacterFromSet:]:
个异常。错误读取
' NSInvalidArgumentException',原因:' - [NSNull length]:
以下是我的代码。
UILabel* spousename = [[UILabel alloc] initWithFrame:CGRectMake(10,line, 300,20)];
[spousename setText:[_thisburial objectForKey:@"Spouse"]];
我尝试使用以下代码测试空字符但是没有捕获错误。尝试执行addSuview指令时它会爆炸。
if(![spousename isEqual:nil])
{
[self.view addSubview:spousename];
}
那么如何在标签中测试NSNull
,以防止收到此[NSNull rangeOfCharacterFromSet:]:
错误消息?
答案 0 :(得分:1)
您的数据实际上并不具有“配偶”的实际值,并且已通过存储NSNull
的实例作为值来指示。你需要检查一下。
这一行:
[spousename setText:[_thisburial objectForKey:@"Spouse"]];
应该更新以检查值。像这样:
id spouse = _thisburial[@"Spouse"];
if ([spouse isKindOfClass:[NSString class]]) {
spousename.text = spouse;
} else {
// The "spouse" value wasn't set or it's not a string
spousename.text = @""; // Set to whatever you want here
}
答案 1 :(得分:0)
[_thisburial objectForKey:@"Spouse"]
正在返回NSNull
。需要特别处理。
NSString *spouseString = [_thisburial objectForKey:@"Spouse"];
if (spouseString == (id)[NSNull null]) {
spouseString = @"";
}
[spousename setText:spouseString];