Identify NULL values in objective C
本问题已经有最佳答案,请猛点这里访问。
我知道我们可以使用 nil 来验证objective C 中的空值。我一直在尝试使用它,但程序每次都会崩溃。
这就是我在服务中得到的响应
1
2 3 4 5 6 7 8 9 10 11 |
2013–12–16 12:45:32.867 Pizza to Go[1857:a0b] 0
2013–12–16 12:45:32.868 Pizza to Go[1857:a0b] <null> 2013–12–16 12:45:32.868 Pizza to Go[1857:a0b] <null> 2013–12–16 12:45:32.868 Pizza to Go[1857:a0b] <null> 2013–12–16 12:45:32.868 Pizza to Go[1857:a0b] <null> 2013–12–16 12:45:32.869 Pizza to Go[1857:a0b] 70 2013–12–16 12:45:32.869 Pizza to Go[1857:a0b] 130 2013–12–16 12:45:32.869 Pizza to Go[1857:a0b] 0 2013–12–16 12:45:32.869 Pizza to Go[1857:a0b] 80 2013–12–16 12:45:32.869 Pizza to Go[1857:a0b] 50.5 2013–12–16 12:45:32.869 Pizza to Go[1857:a0b] 10 |
我第一次尝试这个并崩溃了
1
2 3 4 5 |
if([[result[i] objectForKey:@"ordertotalprice"] isEqualToString:@"<null>"]){
orderHead.price = 0; }else{ orderHead.price = [[result[i] objectForKey:@"ordertotalprice"] doubleValue]; } |
然后这也崩溃了。
1
2 3 4 5 |
if([result[i] objectForKey:@"ordertotalprice"] == nil){
orderHead.price = 0; }else{ orderHead.price = [[result[i] objectForKey:@"ordertotalprice"] doubleValue]; } |
这是来自错误日志
1
2 |
–[NSNull doubleValue]: unrecognized selector sent to instance 0x20af068
2013–12–16 12:55:19.017 Pizza to Go[2083:a0b] *** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[NSNull doubleValue]: unrecognized selector sent to instance 0x20af068’ |
我不确定我在这里做错了什么。一些帮助表示赞赏。
Foundation 集合只能包含对象类型(
尝试将
编辑:但是是的,当只是比较值时
1
2 3 4 5 |
if([result[i] objectForKey:@"ordertotalprice"]){
orderHead.price = 0; }else{ orderHead.price = [[result[i] objectForKey:@"ordertotalprice"] doubleValue]; } |
就足够了。
1
2 3 4 |
if ([[result[i] objectForKey:@"ordertotalprice"] isKindOfClass:[NSNull class]])
{ orderHead.price = 0; } |
1
2 3 4 5 |
if(![result[i] objectForKey:@"ordertotalprice"]){
orderHead.price = 0; }else{ orderHead.price = [[result[i] objectForKey:@"ordertotalprice"] doubleValue]; } |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/268572.html