kCFNull 和 [NSNull null] 可以互换吗
它似乎
if(myObj == (typeOfMyObj *) kCFNull){
//myObj is null
}
并
if(myObj == (typeOfMyObj *) [NSNull null]){
//myObj is null
}
产生相同的结果。
情况总是如此吗?我正在开发一个 iOS 5 应用程序。
谢谢!
It seems to be
if(myObj == (typeOfMyObj *) kCFNull){
//myObj is null
}
and
if(myObj == (typeOfMyObj *) [NSNull null]){
//myObj is null
}
produce the same result.
Is this always the case? I'm developing an iOS 5 application.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
似乎 Apple 的文档都没有声明
NSNull
和CFNullRef
是免费桥接的,但似乎它们自CFNullRef
以来一直都是免费桥接的> 在 Mac OS X 10.2 中引入。NSNull
从 Mac OS X 10.0 开始可用。最后,它们实际上都只是充当杰出 NULL 对象的哨兵值。可能有一种方法可以让您的代码避免依赖
kCFNull == [NSNull null]
,但如果您由于某种原因不能这样做,那么我不会太担心。It appears none of Apple's docs state that
NSNull
andCFNullRef
are toll-free-bridged, but it nevertheless seems that they are and have been sinceCFNullRef
was introduced in Mac OS X 10.2.NSNull
was available starting with Mac OS X 10.0. In the end, they are both really just sentinel values that serve as a distinguished NULL object.There's probably a way for your code to avoid depending on
kCFNull == [NSNull null]
, but if you can't for some reason, then I wouldn't worry about it too much.我同意 Jeremy 的观点,
如果你想 100% 安全,最好的选择是使用 isKindOfClass 检查类:
即:
或者
然后你就可以安全地应对未来的任何更改,尽管它非常乏味并且是所有建议方法中最慢的:P
是这样的问题也不是不可能的
有一天,甚至
,尽管
这些问题导致大多数 Obj-C 开发者花更多的时间在各种麻烦上而不是编写代码 :P
I agree with Jeremy,
If you wanted to be 100% safe, the best bet would be to check the class using isKindOfClass:
ie:
or
Then you're safe against any future changes, though it's super tedious and the slowest of all proposed approaches :P
It's not out of the question that one day
but
or even
Though these are the sorts of concerns that cause most Obj-C devs to spend more time jumping through hoops than writing code :P