kCFNull 和 [NSNull null] 可以互换吗

发布于 2024-12-11 02:19:01 字数 257 浏览 0 评论 0原文

它似乎

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨巷深深 2024-12-18 02:19:01

似乎 Apple 的文档都没有声明 NSNullCFNullRef 是免费桥接的,但似乎它们自 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 and CFNullRef are toll-free-bridged, but it nevertheless seems that they are and have been since CFNullRef 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.

面犯桃花 2024-12-18 02:19:01

我同意 Jeremy 的观点,

如果你想 100% 安全,最好的选择是使用 isKindOfClass 检查类:

即:

[object isKindOfClass:[NSNull class]]

或者

[object isEqual:[NSNull null]]

然后你就可以安全地应对未来的任何更改,尽管它非常乏味并且是所有建议方法中最慢的:P

是这样的问题也不是不可能的

[[NSNull null] isEqual:(id)kCFNull] == YES

有一天,甚至

([NSNull null] == kCFNull) == NO

,尽管

([NSNull null] == [NSNull null]) == NO

这些问题导致大多数 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:

[object isKindOfClass:[NSNull class]]

or

[object isEqual:[NSNull null]]

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

[[NSNull null] isEqual:(id)kCFNull] == YES

but

([NSNull null] == kCFNull) == NO

or even

([NSNull null] == [NSNull null]) == NO

Though these are the sorts of concerns that cause most Obj-C devs to spend more time jumping through hoops than writing code :P

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文