178*_*040 21 json objective-c nsnumber
我的应用程序中有一个JSON解析器,我将值加载到detailDataSourceDict变量中.当我尝试获取数组的valueForKey并尝试将其与0进行比较时,它永远不会...
这是我的代码:
if (indexPath.row == 1) {
NSNumber *rating = [detailDataSourceDict valueForKey:@"rating"];
NSLog(@"Rating: %@",rating);
if (rating == 0) {
cell.detailTextLabel.text = @"This sheet has not yet been rated.";
}
else {
cell.detailTextLabel.text = [NSString stringWithFormat:@"This sheet has a %@ star rating.",rating];
}
cell.textLabel.text = @"Rating";
}
Run Code Online (Sandbox Code Playgroud)
我在我的JSON Feed中看到"评级":"0",但是当评级为0时,它显示"此表格有0星评级."而不是"此表尚未评级".
有什么建议?谢谢.
Reg*_*ent 53
NSNumber *rating是一个对象.0是一种原始类型.可以比较原始类型==.物体不能; 他们需要比较平等使用isEqual:.
因此取而代之:
rating == 0
Run Code Online (Sandbox Code Playgroud)
有:
[rating isEqual:@0]
Run Code Online (Sandbox Code Playgroud)
(@0作为NSNumber文字)
或者:
rating.integerValue == 0
Run Code Online (Sandbox Code Playgroud)
你的错误代码甚至编译的原因0是等于0x0反过来等于nil(等等,节省细节).所以,你当前的代码将等同于:
rating == nil
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13269 次 |
| 最近记录: |