在objective-c中测试null [英] Testing for null in objective-c

查看:56
本文介绍了在objective-c中测试null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在循环中有以下代码

   NSArray * images = [definitionDict objectForKey:@"Images"];
    NSLog(@"images in definitionDict %@", images);
    if (!images )
        NSLog(@"NULL");
    else
        NSLog(@"NOTNULL");

给出以下输出

images in definitionDict (
    "/some/brol/brol.jpg"
)
NOTNULL
images in definitionDict <null>
NOTNULL

我不明白图像数组为空的第二种情况.为什么在我的测试中没有正确检测到这一点?我该如何调试这样的问题?

I do not understand the second case, where the images array is null. Why is this not detected correctly in my test ? How can I debug such a problem ?

推荐答案

不是 nil.nil 打印时会打印 (null).你拥有的是一个 NSNull.NSNull 是一个对象,它只是不响应太多.它可以作为占位符供您使用.

<null> is not nil. nil will print (null) when printed. What you have is an NSNull. NSNull IS an object, it just doesn't respond to much. Its available as a placeholder for you to use.

要测试 NSNull,您可以使用 if ([images isEqual:[NSNull null]])

To test for NSNull you can use if ([images isEqual:[NSNull null]])

请参阅文档了解关于 NSNull

这篇关于在objective-c中测试null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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