测试与 NSNull 的相等性 [英] Testing equality to NSNull

查看:66
本文介绍了测试与 NSNull 的相等性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个代码块,用于测试字典是否为空,如果不是,则拉出正确的对象.然而,出于某种原因,尽管 if 检查失败,代码仍然执行.NSNull 的工作方式是否有一些我不理解的怪癖,或者这是 Apple 的错误?

Below is a code block, that is supposed to test to see if a dictionary is null, and if it isn't, pull out the correct object. However, for some reason, despite the fact that the if check fails, the code still executes. Is there some quirk with how NSNull works that I don't understand, or is this an Apple bug?

if (svcUser && !(svcUser == (id)[NSNull null])) {
    return [svcUser objectForKey:@"access_level"];
}

控制台响应:

(lldb) print svcUser && !(svcUser == (id)[NSNull null])
(bool) $0 = false
(lldb) continue
-[NSNull objectForKey:]: unrecognized selector sent to instance 0x2b51678

推荐答案

只需检查:

svcUser == [NSNull null]

这是 Apple 在其 文档.

This is the approach that Apple mentions in their docs.

这篇关于测试与 NSNull 的相等性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆