Objective-C 中 nil、NIL 和 null 的区别
我想知道 nil
、NIL
和 null
之间的区别。 我用谷歌搜索了一下,发现了这个:
nil
->指向 Objective-C 对象的空指针
NIL
->指向 Objective-C 类的空指针
null
->指向原始类型的空指针或缺少数据
但我无法清楚地理解术语“Objective-C 对象”和“类”。
请向我解释一下。另外,Objective-C 中是否有类似 NSNull
或 NSNil
的单词?如果是这样,请解释一下它的用途。
I want to know the difference between nil
, NIL
, and null
.
I've googled around and found this:
nil
-> null pointer to Objective-C object
NIL
-> null pointer to Objective-C class
null
-> null pointer to primitive type or absence of data
But I'm not able to understand the terms "Objective-C object" and "class" clearly.
Please explain this to me. Also, is there any word like NSNull
or NSNil
in Objective-C? If so, then please explain for what it is for.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
nil
是 Objective-C 对象的文字 null 值,对应于抽象类型id
或通过@interface
声明的任何 Objective-C 类型。例如:Nil
是 Objective-C 类的文字 null 值,对应于类型Class
。由于大多数代码不需要变量来引用类,因此它的使用并不常见。一个例子是:NULL
是任意 C 指针的文字 null 值。例如,NSNull
是表示 null 的对象的类。事实上,只有一个对象,即+[NSNull null]
返回的对象。它与 nil 不同,因为 nil 是一个文字 null 值,即它不是一个对象。另一方面,NSNull
的单个实例是一个正确的对象。NSNull
通常用于 Foundation 集合,因为它们无法存储nil
值。对于字典,-objectForKey:
返回nil
表示给定的键在字典中没有对应的对象,即该键尚未添加到字典中。字典。如果您想明确表示您拥有某个键但还没有值,则可以使用[NSNull null]
。例如,以下代码会引发异常,因为字典无法存储
nil
值:另一方面,以下代码有效,因为
[NSNull null]
是非>nil
对象:值得一提的是,Foundation 集合具有初始值设定项,它使用
nil
作为对象列表末尾的标记,而无需指定列表中的元素数量。发生这种情况的唯一原因是nil
无法存储在 Foundation 集合中。例如,对于
NIL
或NSNil
,Objective-C或Apple Foundation中没有这样的东西。nil
is the literal null value for Objective-C objects, corresponding to the abstract typeid
or any Objective-C type declared via@interface
. For instance:Nil
is the literal null value for Objective-C classes, corresponding to the typeClass
. Since most code doesn’t need variables to reference classes, its use is not common. One example is:NULL
is the literal null value for arbitrary C pointers. For instance,NSNull
is a class for objects that represent null. In fact, there’s only one object, namely the one returned by+[NSNull null]
. It is different fromnil
becausenil
is a literal null value, i.e., it isn’t an object. The single instance ofNSNull
, on the other hand, is a proper object.NSNull
is often used in Foundation collections since they cannot storenil
values. In the case of dictionaries,-objectForKey:
returnsnil
to indicate that a given key has no corresponding object in the dictionary, i.e., the key hasn’t been added to the dictionary. If you want to make it explicit that you have a certain key but it doesn’t have a value yet, you can use[NSNull null]
.For instance, the following throws an exception because dictionaries cannot store
nil
values:On the other hand, the following code is valid since
[NSNull null]
is a non-nil
object:It’s worth mentioning that Foundation collections have initialisers that use
nil
as a marker for the end of a list of objects without having to specify the number of elements in the list. This can only happen becausenil
cannot be stored in a Foundation collection. For instance,As for
NIL
orNSNil
, there are no such things in Objective-C or Apple Foundation.我不确定,但我认为
nil
只能用来代替id
,Java 和 C++ 程序员将其视为指针
到一个对象
。对于非对象指针使用NULL
。nil
通常用于 Objective-C 对象类型,而NULL
用于 C 风格指针I am not sure but i think
nil
should only be used in place of anid
, what Java and C++ programmers would think of as apointer
to anobject
. UseNULL
for non-object pointers.nil
is usually used for an Objective-C object type, whileNULL
is used for c-style pointers一起使用
Nil、Null 和 nil 与下面的 1> Objective c Class
2> 无Objective c 对象
3> 为 nil C 指针为空
示例:
1>Class A=Nil;
2>NSString strName=nil;
3>char *pointerChar = NULL;
Nil,Null and nil are used with below
1> Nil for Objective c Class
2> nil for Objective c object
3> Null for C pointer
Example:
1>Class A=Nil;
2>NSString strName=nil;
3>char *pointerChar = NULL;
假设你有一个类
MyClass
那么按照约定,如果您想将其实例初始化为 null 值,则使用
nil
(与 java 中的null
相同)即
如果你想将一个原始指针初始化为空值(与c中相同),你可以使用
,如果你想初始化对空值的
Class
引用(与null
in java) then use这只是一个约定,否则 Nil 或 nil 具有相同的含义,也许 NULL 、 nil 或 Nil 都是相同的。
的定义
以下是
objc.h
文件和stddef.h
中以及
_types.h< 中
__DARWIN_NULL
的定义/code>所以逻辑上没有区别。这里的主要思想是将
C
或Objective-C
指针初始化为0
。如果您了解 C 语言,那么您可以在不进行类型转换的情况下将
0
分配给指针。因为您不需要类型转换0
来将其分配给指针。简而言之,它们都是
0
,仅此而已。Suppose you have a class
MyClass
then by convention
nil
is used if you want to initialize its instance to null value (same asnull
in java)i.e.
and if you want to initialize a primitive pointer to null value (same as in c) you use
and if you want to initialize to
Class
reference to null value (same asnull
in java) then useIt's just a convention otherwise Nil or nil have same meaning and perhaps NULL , nil or Nil all are same.
Here is the definition for these in
objc.h
fileAnd in
stddef.h
And the definition of
__DARWIN_NULL
in_types.h
So there is no difference logically. The main idea here is to initialize a pointer whether
C
orObjective-C
to0
. If you have knowledge ofC
then you can assignwithout type casting
0
to a pointer. As you don't need to typecast0
to assign it to a pointer.In short they all are
0
and nothing else.这将帮助您理解 nil、NIL 和 null 之间的区别。
以下链接可能会以某种方式帮助您:
http://nshipster.com/nil/
以下是一些重要信息部分来自链接:
This will help you to understand the difference between nil,NIL and null.
The below link may help you in some way:
http://nshipster.com/nil/
Here is some important part from the link:
nil、NIL 和 null。这取决于你的要求。
NSNull
集合,如
NSArray
和NSDictionary
不能包含 nil 值。nil
该对象指向其他对象的所有指针都以 nil 开头,因此不需要在 init 方法中设置 self.(association) = nil 等。
在其他语言中,比如 C++,这会让你的程序崩溃,但在 Objective-C 中,调用 nil 上的方法会返回零值。
符号值含义
Objective-C 对象的 nil (id)0 文字 null 值 Objective
-C 类的 nil (Class)0 文字 null 值
nil, NIL and null. is depended on your requirement.
NSNull
collections like
NSArray
andNSDictionary
not being able to contain nil values.nil
all pointers that object has to other objects begin as nil, so it's unnecessary to, for instance, set self.(association) = nil in init methods.
In other languages, like C++, this would crash your program, but in Objective-C, invoking a method on nil returns a zero value.
Symbol Value Meaning
nil (id)0 literal null value for Objective-C objects
Nil (Class)0 literal null value for Objective-C classes