0

[NSNull rangeOfCharacterFromSet:]:尝试显示标签时出现异常。错误读取

'NSInvalidArgumentException',原因:'-[NSNull 长度]:

下面是我的代码。

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:]:错误消息?

4

2 回答 2

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
}
于 2016-04-22T17:11:31.450 回答
0

[_thisburial objectForKey:@"Spouse"]正在返回一个NSNull. 需要对此进行特殊处理。

NSString *spouseString = [_thisburial objectForKey:@"Spouse"];
if (spouseString == (id)[NSNull null]) {
    spouseString = @"";
}

[spousename setText:spouseString];
于 2016-04-22T17:12:01.433 回答