NSAssert 不起作用
我试图在我的代码中使用 NSAssert 但它没有做任何事情。
在这段代码中,断言应该失败,但实际上却没有:
MSLog(@"cross.obj = %@",[cross obj]);
NSAssert([cross obj]!=nil,@"[cross obj] == nil");
其输出是:
cross.obj = (空)
可能是什么问题?
I'm trying to use NSAssert
in my code but it doesn't do a thing.
In this piece of code, the assertion should fail but doesn't:
MSLog(@"cross.obj = %@",[cross obj]);
NSAssert([cross obj]!=nil,@"[cross obj] == nil");
The output of this is:
cross.obj = (null)
What could be the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NS_BLOCK_ASSERTIONS
阻止断言发挥作用。尝试删除定义。NS_BLOCK_ASSERTIONS
blocks assertions from functioning. Try deleting the definition.其背后的原因是
nil
和null
值之间存在差异。因此,请通过其他示例进行检查。
它会起作用的。
The reason behind this is that there is a difference between a
nil
and anull
value.So please check it with other examples.
It will work.
原因可能是
[cross obj]
的结果是[NSNull null]
,而不是nil
。输出可能是
,这并不意味着NULL
或nil
而是NSNull
单例对象。该对象用作占位符,其中nil
不是一个选项(例如在NSArray
中)。另一个原因可能是定义了
NS_BLOCK_ASSERTIONS
预处理器宏,该宏禁用了NSAssert
。The reason might be that the result from
[cross obj]
is[NSNull null]
, notnil
.The output probably is
<null>
which does not meanNULL
ornil
but theNSNull
singleton object. This object is used as a placeholder wherenil
is not an option (for instance inNSArray
s).Another reason might be that the
NS_BLOCK_ASSERTIONS
preprocessor macro is defined which disablesNSAssert
.