#define Nil __DARWIN_NULL /* id of Nil class */
#define nil __DARWIN_NULL /* id of Nil instance */
#define NULL __DARWIN_NULL
#define __DARWIN_NULL ((void *)0)
if (anObject == nil) { // One cannot compare nothing to nothing,
// that wouldn't make sense.
if (anObject) { // Correct, one checks for the existence of anObject
Objective-C objects
First of all, when you call this:
id someObject = [NSArray array];
someObject isn't the array object directly but only a pointer to it. That means, if someObject is equal to 0x1234 there's an object at that address in the memory.
That's the reason why
id someOtherObject = someObject;
doesn't copy the object. Both pointers point now to the same object.
Pointer to 0x0
So, how is nil defined? Let's take a look at the source code:
objc.h
#define nil __DARWIN_NULL /* id of Nil instance */
In Objective-C, it is valid to send a
message to nil—it simply has no effect
at runtime. There are several patterns
in Cocoa that take advantage of this
fact. The value returned from a
message to nil may also be valid: …
The returned values are either nil, 0 or a struct with all variables initialized to 0. Which one it is depends on the expected return type. There is an explicit check in the objective-c runtime for messages to nil, that means it's really fast.
Nil, nil, NULL
Those are the 3 types. Here are all the definitions:
#define Nil __DARWIN_NULL /* id of Nil class */
#define nil __DARWIN_NULL /* id of Nil instance */
#define NULL __DARWIN_NULL
#define __DARWIN_NULL ((void *)0)
As can be seen, they are all exactly the same. Nil and nil are defined by Objective-C, NULL comes from C.
What's the difference then? It's only about style. It makes the code more readable.
Nil is used as a non-existent class: Class someClass = Nil.
nil is used as a non-existent instance: id someInstance = nil.
NULL is a pointer to a non-existent memory part: char *theString = NULL.
Short
nil isn't an empty object but a non-existent one. A method -getSomeObject doesn't return an empty object if it doesn't exist but returns nil which tells the user that there is no object.
Maybe this makes sense: (Both would compile and run.)
if (anObject == nil) { // One cannot compare nothing to nothing,
// that wouldn't make sense.
if (anObject) { // Correct, one checks for the existence of anObject
Formally defined, it is as Joshual defined - a pointer to nothing or a pointer to no object at all.
From a practical, implementation perspective, particularly when dealing with data structures and algorithms, a nill often will represent a sentinel in a data structure or an object that represents "nothing" for example, in a red-black tree, technically, all of the leaf nodes are "nill", but they still have the same or similar properties & operations of a leaf node (color, pointer to a parent, etc.) - in those cases, it is really a "nothing object" ... if that makes any sense.
So, formally, it is a pointer to nothing, in practice, it is often treated as a representation of nothing, but it is never ... null.
One useful way of thinking about nil is imagining that it's the empty object that "does nothing".
By "does nothing" I mean, any message you send it won't have side effects.
It's "empty" in the sense that when you ask it for the value of any property, it always returns nil. So it's not holding any values --> it's empty.
(Actually, it's a little more complicated than that, when you ask for the value of a property that returns a type that's NOT an obj-c object. You will get back a pointer-sized 0. So for any scalar values that are no larger than sizeof(void*) you get 0. But if you ask for a struct or a double on a 32 bit system, you get an undefined result. I've written about this here.)
Maybe I'm just missing the obvious but this seems like a semantic question. As in, you can use lots of different words to describe it, but your description works well enough already.
Just to clarify, in Objective-C nil and null are not the same thing. Neither do they represent the same thing in Objective-C as they do in mathematics or other programming languages.
Nil is actually a special memory address of 0x0 (at least the compiler treats it as an address.) Nil is used as the address of an object that is named but not allocated. This allows for test for the existence/allocation of named objects as well as providing a safe way to send messages to objects that might not exist. While in math and some languages you can compare scalar variables to nil, in Objective-c, you should not. You should only compare the address of objects.
NULL by contrast can mean either the standard C defined as an integer value ofNULL==0 or it can represents an actual allocated object of the class NSNull with a specific address in memory. NSNull is however, a singleton object i.e. only one exist for every application. It is used as placeholder for other objects usually in collections. You cannot use it in comparisons with any other object. You can only check if a particular pointer points to the singleton NSNull object. Note however that as an allocated object[NSNull null]!=NULL,
The confusion between nil and NULL arises because assigning an address of NULL assigns it to the nil address of 0x0. Such code works but can cause subtle problem at times. It's best to get in the habit of not confusing the two.
So, in Objective-C, nil, NUll and NSNull are three different but overlapping concepts that are easy to confuse. Nil should be used for addresses, NULL should be used for scalar values and NSNull should be used as a placeholder for allocated objects.
发布评论
评论(9)
Objective-C 对象
首先,当你调用这个时:
someObject
并不是直接的数组对象,而只是指向它的指针。这意味着,如果someObject
等于0x1234
内存中该地址处就有一个对象。原因
这就是为什么不复制对象的 。两个指针现在都指向同一个对象。
指向 0x0 的指针
那么,nil 是如何定义的呢?我们看一下源代码:
objc.h
_types.h
看起来
nil
是一个指向地址0x0的指针。所以呢?
让我们看看Objective-C 编程参考 不得不说:
返回值是
nil
、0 或所有变量都初始化为 0 的struct
。具体是哪一个取决于预期返回类型。 Objective-C 运行时会显式检查nil
消息,这意味着它非常快。Nil
、nil
、NULL
这是 3 种类型。以下是所有定义:
可以看出,它们都是完全相同的。
Nil
和nil
是Objective-C定义的,NULL
来自C。那有什么区别呢?这只是风格的问题。它使代码更具可读性。
Nil
用作不存在的类:Class someClass = Nil
。nil
用作不存在的实例:id someInstance = nil
。NULL
是指向不存在的内存部分的指针:char *theString = NULL
。Short
nil
不是一个空对象,而是一个不存在的对象。如果对象不存在,-getSomeObject
方法不会返回空对象,而是返回nil
,告诉用户不存在对象。也许这是有道理的:(两者都会编译并运行。)
Objective-C objects
First of all, when you call this:
someObject
isn't the array object directly but only a pointer to it. That means, ifsomeObject
is equal to0x1234
there's an object at that address in the memory.That's the reason why
doesn't copy the object. Both pointers point now to the same object.
Pointer to 0x0
So, how is
nil
defined? Let's take a look at the source code:objc.h
_types.h
Looks like
nil
is a pointer to the address 0x0.So what?
Let's see what the Objective-C Programming Reference has to say:
The returned values are either
nil
, 0 or astruct
with all variables initialized to 0. Which one it is depends on the expected return type. There is an explicit check in the objective-c runtime for messages tonil
, that means it's really fast.Nil
,nil
,NULL
Those are the 3 types. Here are all the definitions:
As can be seen, they are all exactly the same.
Nil
andnil
are defined by Objective-C,NULL
comes from C.What's the difference then? It's only about style. It makes the code more readable.
Nil
is used as a non-existent class:Class someClass = Nil
.nil
is used as a non-existent instance:id someInstance = nil
.NULL
is a pointer to a non-existent memory part:char *theString = NULL
.Short
nil
isn't an empty object but a non-existent one. A method-getSomeObject
doesn't return an empty object if it doesn't exist but returnsnil
which tells the user that there is no object.Maybe this makes sense: (Both would compile and run.)
它不是一个空物体,而是根本没有任何物体。其余的答案涵盖了其他语义,所以我就这样吧:)
It's not an empty object, it's the lack of any object at all. The rest of the answers cover the other semantics, so I'll leave it at that :)
nil 只能与指针一起使用,nil 表示指向无的指针(其值为零)
nil should only be used with pointers, and nil represents a pointer which points to nothing (it's value is zero)
没什么”。但是,您可以向“nothing”发送消息,而不会像尝试调用
NULL
上的方法一样被杀死。您可以查看此问题以了解有关
NULL<的更多信息/code> 与
nil
It's "nothing". But a "nothing" you can send messages to without getting killed as you would if you were trying to call a method on
NULL
.You can look at this question to have more info on
NULL
vs.nil
它是一个空指针 - 一个指向“无”的指针。
It's a null pointer - a pointer to "nothing".
正式定义,它是 Joshua 定义的——一个不指向任何东西的指针,或者一个根本不指向任何对象的指针。
从实际实现的角度来看,特别是在处理数据结构和算法时,零通常代表数据结构中的哨兵或代表“无”的对象,例如在红黑树中,从技术上讲,所有叶节点是“nil”,但它们仍然具有相同或相似的属性&叶节点的操作(颜色、指向父节点的指针等) - 在这些情况下,它实际上是一个“无对象”......如果这有意义的话。
因此,形式上,它是一个指向空的指针,在实践中,它通常被视为空的表示,但它永远不会......空。
Formally defined, it is as Joshual defined - a pointer to nothing or a pointer to no object at all.
From a practical, implementation perspective, particularly when dealing with data structures and algorithms, a nill often will represent a sentinel in a data structure or an object that represents "nothing" for example, in a red-black tree, technically, all of the leaf nodes are "nill", but they still have the same or similar properties & operations of a leaf node (color, pointer to a parent, etc.) - in those cases, it is really a "nothing object" ... if that makes any sense.
So, formally, it is a pointer to nothing, in practice, it is often treated as a representation of nothing, but it is never ... null.
思考
nil
的一种有用方法是想象它是“不执行任何操作”的空对象。我所说的“不执行任何操作”的意思是,您发送的任何消息都不会产生副作用。
它是“空”的,因为当您向它询问任何属性的值时,它总是返回
nil
。所以它不包含任何值 -->它是空的。(实际上,情况比这更复杂一点,当您请求返回不是 obj-c 对象的类型的属性值时。您将返回一个指针大小的
0
。所以对于任何不大于sizeof(void*)
的标量值,您将得到0
但如果您要求struct
或。 >double
在 32 位系统上,您会得到未定义的结果,我已经写过此处。)One useful way of thinking about
nil
is imagining that it's the empty object that "does nothing".By "does nothing" I mean, any message you send it won't have side effects.
It's "empty" in the sense that when you ask it for the value of any property, it always returns
nil
. So it's not holding any values --> it's empty.(Actually, it's a little more complicated than that, when you ask for the value of a property that returns a type that's NOT an obj-c object. You will get back a pointer-sized
0
. So for any scalar values that are no larger thansizeof(void*)
you get0
. But if you ask for astruct
or adouble
on a 32 bit system, you get an undefined result. I've written about this here.)也许我只是错过了显而易见的事情,但这似乎是一个语义问题。例如,您可以使用许多不同的词语来描述它,但您的描述已经足够好了。
http://en.wikipedia.org/wiki/Null_(computer_programming)
Maybe I'm just missing the obvious but this seems like a semantic question. As in, you can use lots of different words to describe it, but your description works well enough already.
http://en.wikipedia.org/wiki/Null_(computer_programming)
澄清一下,在 Objective-C 中 nil 和 null 不是一回事。它们在 Objective-C 中所表示的事物与在数学或其他编程语言中所表示的事物也不同。
Nil实际上是一个特殊的内存地址0x0(至少编译器将其视为地址)。Nil用作已命名但未分配的对象的地址。这允许测试命名对象的存在/分配,并提供一种安全的方法来向可能不存在的对象发送消息。在数学和某些语言中,您可以将标量变量与 nil 进行比较,但在 Objective-c 中,您不应该这样做。您应该只比较对象的地址。
相比之下,NULL 可以表示标准 C 定义为
NULL==0
的整数值,也可以表示类 NSNull ,在内存中具有特定地址。然而,NSNull 是一个单例对象,即每个应用程序仅存在一个对象。它通常用作集合中其他对象的占位符。您不能将它与任何其他对象进行比较。您只能检查特定指针是否指向单例 NSNull 对象。但请注意,作为分配的对象[NSNull null]!=NULL
,nil 和 NULL 之间会产生混淆,因为分配 NULL 地址会将其分配给 0x0 的 nil 地址。此类代码可以工作,但有时会导致微妙的问题。最好养成不要混淆两者的习惯。
所以,在 Objective-C 中,nil、NUll 和 NSNull 是三个不同但又重叠的概念,很容易混淆。 Nil 应用于地址,NULL 应用于标量值,NSNull 应用于分配对象的占位符。
Just to clarify, in Objective-C nil and null are not the same thing. Neither do they represent the same thing in Objective-C as they do in mathematics or other programming languages.
Nil is actually a special memory address of 0x0 (at least the compiler treats it as an address.) Nil is used as the address of an object that is named but not allocated. This allows for test for the existence/allocation of named objects as well as providing a safe way to send messages to objects that might not exist. While in math and some languages you can compare scalar variables to nil, in Objective-c, you should not. You should only compare the address of objects.
NULL by contrast can mean either the standard C defined as an integer value of
NULL==0
or it can represents an actual allocated object of the class NSNull with a specific address in memory. NSNull is however, a singleton object i.e. only one exist for every application. It is used as placeholder for other objects usually in collections. You cannot use it in comparisons with any other object. You can only check if a particular pointer points to the singleton NSNull object. Note however that as an allocated object[NSNull null]!=NULL
,The confusion between nil and NULL arises because assigning an address of NULL assigns it to the nil address of 0x0. Such code works but can cause subtle problem at times. It's best to get in the habit of not confusing the two.
So, in Objective-C, nil, NUll and NSNull are three different but overlapping concepts that are easy to confuse. Nil should be used for addresses, NULL should be used for scalar values and NSNull should be used as a placeholder for allocated objects.