什么最能描述 nil?那真的是什么? [英] What describes nil best? What's that really?
问题描述
目前我将其理解为一种空对象".但它到底是什么?
Currently I understand it as a kind of "empty object". But what's it really?
推荐答案
Objective-C 对象
首先,当你调用它时:
Objective-C objects
First of all, when you call this:
id someObject = [NSArray array];
someObject
不是直接的数组对象,而是指向它的指针.这意味着,如果 someObject
等于 0x1234
,则内存中该地址处有一个对象.
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.
这就是为什么
id someOtherObject = someObject;
不复制对象.两个指针现在都指向同一个对象.
doesn't copy the object. Both pointers point now to the same object.
那么,nil
是如何定义的?我们来看一下源码:
So, how is nil
defined? Let's take a look at the source code:
objc.h
#define nil __DARWIN_NULL /* id of Nil instance */
_types.h
#ifdef __cplusplus
…
#else /* ! __cplusplus */
#define __DARWIN_NULL ((void *)0)
#endif /* __cplusplus */
看起来 nil
是指向地址 0x0 的指针.
Looks like nil
is a pointer to the address 0x0.
让我们看看 Objective-C 编程参考 不得不说:
Let's see what the Objective-C Programming Reference has to say:
在 Objective-C 中,发送一个消息为零 - 它根本没有效果在运行时.有几种模式在可可中利用了这一点事实.从 a 返回的值到 nil 的消息也可能是有效的:...
Sending Messages to nil
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: …
返回值是nil
、0 或struct
,所有变量都初始化为0.它是哪一个取决于预期的返回类型.在objective-c 运行时有一个显式检查nil
的消息,这意味着它真的很快.
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.
那是3种类型.以下是所有定义:
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)
可以看出,它们都完全相同.Nil
和 nil
由 Objective-C 定义,NULL
来自 C.
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
用作不存在的类:Class someClass = Nil
.nil
用作不存在的实例:id someInstance = nil
.NULL
是指向不存在的内存部分的指针:char *theString = NULL
.
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
.
nil
不是一个空对象,而是一个不存在的对象.方法 -getSomeObject
如果不存在则不会返回空对象,而是返回 nil
告诉用户没有对象.
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
这篇关于什么最能描述 nil?那真的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!