xcode nil 和 Nil 有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5948910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:00:42  来源:igfitidea点击:

What's the Difference Between nil and Nil

objective-cxcode

提问by user4951

In Objective C?

在目标 C 中?

Are they really the same thing?

他们真的是一回事吗?

How to test that an object is nil?

如何测试一个对象是否为零?

回答by Itai Ferber

Niland nilare defined to be the same thing (__DARWIN_NULL), and are meant to signify nullity (a null pointer, like NULL). Nilis meant for class pointers, and nilis meant for object pointers (you can read more on it in objc.h; search for Nil). Finally, you can test for a null value like this:

Nilnil被定义为相同的东西 ( __DARWIN_NULL),并且意味着表示无效(空指针,如NULL)。Nil用于类指针,nil用于对象指针(您可以在objc.h 中阅读更多相关信息;搜索Nil)。最后,您可以像这样测试空值:

if (object == nil)

or like this:

或者像这样:

if (!object)

since boolean evaluations will make any valid pointer that contains an object evaluate to true.

因为布尔评估将使包含对象的任何有效指针评估为真。

回答by JeremyP

nilis the Objective-C constant for a null pointer to an object, Nilis identically defined. In practice, it is has the same value as the C constant NULL. Test for a nil object like this:

nil是指向对象的空指针的 Objective-C 常量,Nil定义相同。实际上,它与 C 常量具有相同的值NULL。像这样测试一个 nil 对象:

if (fooObj == nil)

In my code, I tend to use nilfor Objective-C objects and NULLfor other C pointers. This is a matter of personal taste - currently nil and NULL are interchangeable for comparison in all existing Objective-C implementations.

在我的代码中,我倾向于使用nilObjective-C 对象和NULL其他 C 指针。这是个人喜好的问题——目前 nil 和 NULL 在所有现有的 Objective-C 实现中可以互换以进行比较。

回答by Carlos Avalos

  • nil(all lower-case) is a null pointer to an Objective-C object.
  • Nil(capitalized) is a null pointer to an Objective-C class.
  • NULL(all caps) is a null pointer to anything else.
  • nil(全部小写)是一个指向 Objective-C 对象的空指针。
  • Nil(大写)是指向 Objective-C 类的空指针。
  • NULL(全部大写)是指向其他任何内容的空指针。