关于 ios:识别objective C 中的 NULL 值 | 珊瑚贝

Identify NULL values in objective C

本问题已经有最佳答案,请猛点这里访问。


我知道我们可以使用 nil 来验证objective C 中的空值。我一直在尝试使用它,但程序每次都会崩溃。

这就是我在服务中得到的响应

1
2
3
4
5
6
7
8
9
10
11
20131216 12:45:32.867 Pizza to Go[1857:a0b] 0
20131216 12:45:32.868 Pizza to Go[1857:a0b] <null>
20131216 12:45:32.868 Pizza to Go[1857:a0b] <null>
20131216 12:45:32.868 Pizza to Go[1857:a0b] <null>
20131216 12:45:32.868 Pizza to Go[1857:a0b] <null>
20131216 12:45:32.869 Pizza to Go[1857:a0b] 70
20131216 12:45:32.869 Pizza to Go[1857:a0b] 130
20131216 12:45:32.869 Pizza to Go[1857:a0b] 0
20131216 12:45:32.869 Pizza to Go[1857:a0b] 80
20131216 12:45:32.869 Pizza to Go[1857:a0b] 50.5
20131216 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
20131216 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 集合只能包含对象类型(NSObject 子类)。因此,为了表示 nil,我们使用 +[NSNull null]。碰巧 NSNull 的 -description 实现返回 <null>,这就是您在控制台中看到的。

尝试将 -objectForKey 的结果与 [NSNull null] 进行比较。


NSDictionary/NSMutableDictionary 接受 [NSNull null] 以指示空条目。

编辑:但是是的,当只是比较值时

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;
   }
  • 类比较在这里是多余的。 +null 被实现为单例,它总是满足简单的指针比较。


1
2
3
4
5
if(![result[i] objectForKey:@“ordertotalprice”]){
    orderHead.price = 0;
}else{
    orderHead.price = [[result[i] objectForKey:@“ordertotalprice”] doubleValue];
}

来源:https://www.codenong.com/20605334/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9573.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?