Objective-C中nil,NIL和null之间的差异

Objective-C中nil,NIL和null之间的差异

问题描述:

我想知道nilNILnull之间的区别. 我在Google上四处搜寻,发现了这一点:

I want to know the difference between nil, NIL and null. I've googled around and found this:

nil->指向Objective-C对象的空指针

nil -> null pointer to Objective-C object

NIL->指向Objective-C类的空指针

NIL -> null pointer to Objective-C class

null->指向原始类型或缺少数据的空指针

null -> null pointer to primitive type or absence of data

但是我不能清楚地理解术语"Objective-C对象"和类".

But I'm not able to understand the terms "Objective-C object" and "class" clearly.

请向我解释一下.另外,在Objective-C中是否有像NSNullNSNil这样的词?如果是这样,请说明其用途.

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.

nil是Objective-C对象的文字空值,对应于抽象类型id或通过@interface声明的任何Objective-C类型.例如:

nil is the literal null value for Objective-C objects, corresponding to the abstract type id or any Objective-C type declared via @interface. For instance:

NSString *someString = nil;
NSURL *someURL = nil;
id someObject = nil;

if (anotherObject == nil) // do something

Nil是Objective-C类的文字空值,对应于类型Class.由于大多数代码不需要变量来引用类,因此其用法并不常见.一个例子是:

Nil is the literal null value for Objective-C classes, corresponding to the type Class. Since most code doesn’t need variables to reference classes, its use is not common. One example is:

Class someClass = Nil;
Class anotherClass = [NSString class];

NULL是任意C指针的原义null值.例如,

NULL is the literal null value for arbitrary C pointers. For instance,

int *pointerToInt = NULL;
char *pointerToChar = NULL;
struct TreeNode *rootNode = NULL;

NSNull是代表空值的对象的类.实际上,只有一个对象,即+[NSNull null]返回的对象.它与nil不同,因为nil是字面量为null的值,即它不是对象.另一方面,NSNull的单个实例是一个适当的对象.

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 from nil because nil is a literal null value, i.e., it isn’t an object. The single instance of NSNull, on the other hand, is a proper object.

NSNull通常用于Foundation集合中,因为它们不能存储nil值.对于字典,-objectForKey:返回nil表示给定键在字典中没有对应的对象,即该键尚未添加到字典中.如果要明确表明您具有某个键,但还没有值,则可以使用[NSNull null].

NSNull is often used in Foundation collections since they cannot store nil values. In the case of dictionaries, -objectForKey: returns nil 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].

例如,以下内容引发异常,因为字典无法存储nil值:

For instance, the following throws an exception because dictionaries cannot store nil values:

NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:nil forKey:@"someKey"];

另一方面,由于[NSNull null]是非nil对象,因此以下代码有效:

On the other hand, the following code is valid since [NSNull null] is a non-nil object:

NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:[NSNull null] forKey:@"someKey"];

值得一提的是,Foundation集合中的初始化程序使用nil作为对象列表结尾的标记,而不必指定列表中元素的数量.因为nil不能存储在Foundation集合中,所以只能发生这种情况.例如,

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 because nil cannot be stored in a Foundation collection. For instance,

NSArray *array = [NSArray arrayWithObjects:@"one", @"two", nil];

对于NILNSNil,在Objective-C或Apple Foundation中都没有这种东西.

As for NIL or NSNil, there are no such things in Objective-C or Apple Foundation.