Objective-C 中 nil、NIL 和 null 的区别

发布于 2024-11-05 18:08:44 字数 376 浏览 1 评论 0原文

我想知道 nilNILnull 之间的区别。 我用谷歌搜索了一下,发现了这个:

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

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

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

但我无法清楚地理解术语“Objective-C 对象”和“类”。

请向我解释一下。另外,Objective-C 中是否有类似 NSNullNSNil 的单词?如果是这样,请解释一下它的用途。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

堇年纸鸢 2024-11-12 18:08:44

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

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

if (anotherObject == nil) // do something

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

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

NULL 是任意 C 指针的文字 null 值。例如,

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

NSNull 是表示 null 的对象的类。事实上,只有一个对象,即 +[NSNull null] 返回的对象。它与 nil 不同,因为 nil 是一个文字 null 值,即它不是一个对象。另一方面,NSNull 的单个实例是一个正确的对象。

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

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

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

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

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

值得一提的是,Foundation 集合具有初始值设定项,它使用 nil 作为对象列表末尾的标记,而无需指定列表中的元素数量。发生这种情况的唯一原因是 nil 无法存储在 Foundation 集合中。例如,

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

对于NILNSNil,Objective-C或Apple Foundation中没有这样的东西。

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 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 is the literal null value for arbitrary C pointers. For instance,

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

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 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].

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

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

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"];

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];

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

冬天的雪花 2024-11-12 18:08:44

我不确定,但我认为 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 an id, what Java and C++ programmers would think of as a pointer to an object. Use NULL for non-object pointers.

nil is usually used for an Objective-C object type, while NULL is used for c-style pointers

心的憧憬 2024-11-12 18:08:44

一起使用

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;

_畞蕅 2024-11-12 18:08:44

假设你有一个类 MyClass
那么按照约定,如果您想将其实例初始化为 null 值,则使用 nil (与 java 中的 null 相同)

MyClass *obj = nil;

如果你想将一个原始指针初始化为空值(与c中相同),你可以使用

int *ptr = NULL; 

,如果你想初始化对空值的Class引用(与null in java) then use

Class classRefOfMyClass = Nil;

这只是一个约定,否则 Nil 或 nil 具有相同的含义,也许 NULL 、 nil 或 Nil 都是相同的。

的定义

#ifndef Nil
# if __has_feature(cxx_nullptr)
#   define Nil nullptr
# else
#   define Nil __DARWIN_NULL
# endif
#endif

#ifndef nil
# if __has_feature(cxx_nullptr)
#   define nil nullptr
# else
#   define nil __DARWIN_NULL
# endif
#endif

以下是 objc.h 文件和 stddef.h

#define NULL ((void*)0)

以及 _types.h< 中 __DARWIN_NULL 的定义/code>

#define __DARWIN_NULL ((void *)0)

所以逻辑上没有区别。这里的主要思想是将 CObjective-C 指针初始化为 0。如果您了解 C 语言,那么您可以

int *ptr = 0;

在不进行类型转换的情况下将 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 as null in java)
i.e.

MyClass *obj = nil;

and if you want to initialize a primitive pointer to null value (same as in c) you use

int *ptr = NULL; 

and if you want to initialize to Class reference to null value (same as null in java) then use

Class classRefOfMyClass = Nil;

It'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 file

#ifndef Nil
# if __has_feature(cxx_nullptr)
#   define Nil nullptr
# else
#   define Nil __DARWIN_NULL
# endif
#endif

#ifndef nil
# if __has_feature(cxx_nullptr)
#   define nil nullptr
# else
#   define nil __DARWIN_NULL
# endif
#endif

And in stddef.h

#define NULL ((void*)0)

And the definition of __DARWIN_NULL in _types.h

#define __DARWIN_NULL ((void *)0)

So there is no difference logically. The main idea here is to initialize a pointer whether C or Objective-C to 0. If you have knowledge of C then you can assign

int *ptr = 0;

without type casting 0 to a pointer. As you don't need to typecast 0 to assign it to a pointer.

In short they all are 0 and nothing else.

ま柒月 2024-11-12 18:08:44

这将帮助您理解 nil、NIL 和 null 之间的区别。

所有这三个值都表示空值或零指针值。这
区别在于,NULL 代表任何指针零, nil
特定于对象(例如,id),Nil特定于类指针。
使用正确的 null 应该被认为是一种最佳实践
出于记录目的,在正确的情况下对象,甚至
尽管没有什么可以阻止某人混合和匹配
他们一起走。

以下链接可能会以某种方式帮助您:

http://nshipster.com/nil/

以下是一些重要信息部分来自链接:

在此处输入图像描述

This will help you to understand the difference between nil,NIL and null.

All three of these values represent null, or zero pointer, values. The
difference is that while NULL represents zero for any pointer, nil is
specific to objects (e.g., id) and Nil is specific to class pointers.
It should be considered a best practice of sorts to use the right null
object in the right circumstance for documentation purposes, even
though there is nothing stopping someone from mixing and matching as
they go along.

The below link may help you in some way:

http://nshipster.com/nil/

Here is some important part from the link:

enter image description here

肩上的翅膀 2024-11-12 18:08:44

nil、NIL 和 null。这取决于你的要求。

NSNull

集合,如 NSArrayNSDictionary 不能包含 nil 值。

NSMutableDictionary *MymutableDictionary = [NSMutableDictionary dictionary];
MymutableDictionary[@"someKey"] = [NSNull null]; // Sets value of NSNull singleton for "someKey"
NSLog(@"Keys: %@", [mutableDictionary allKeys]);

nil

该对象指向其他对象的所有指针都以 nil 开头,因此不需要在 init 方法中设置 self.(association) = nil 等。

在其他语言中,比如 C++,这会让你的程序崩溃,但在 Objective-C 中,调用 nil 上的方法会返回零值。

if (name != 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 and NSDictionary not being able to contain nil values.

NSMutableDictionary *MymutableDictionary = [NSMutableDictionary dictionary];
MymutableDictionary[@"someKey"] = [NSNull null]; // Sets value of NSNull singleton for "someKey"
NSLog(@"Keys: %@", [mutableDictionary allKeys]);

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.

if (name != nil)
{
........
}

Symbol Value Meaning

nil (id)0 literal null value for Objective-C objects

Nil (Class)0 literal null value for Objective-C classes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文