Objective-C:NULL、nil 和 @"" 之间有什么区别?

发布于 2024-10-04 03:37:24 字数 402 浏览 6 评论 0 原文

正如标题所说, NULLnil@"" 之间有什么区别?

例如,如果我想检查字典中的字符串是否为空。

我应该使用哪个条件?

if ([dictionary objectForKey:@"aString"] == nil)

if [[dictionary objectForKey:@"aString"] isEqualToString:@""]

if ([dictionary objectForKey:@"aString"] == NULL)

哪一个是正确的?

As the title says, what's the difference between NULL, nil and @"" ?

For example, if I want to check a string in a dictionary is empty.

Which condition should I use ?

if ([dictionary objectForKey:@"aString"] == nil)

or

if [[dictionary objectForKey:@"aString"] isEqualToString:@""]

or

if ([dictionary objectForKey:@"aString"] == NULL)

Which one is right ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

淡写薰衣草的香 2024-10-11 03:37:24

nil 是 NULL 与 Objective-C 对象的类似物。实际上它们是相同的:

 //MacTypes.h
 #define nil NULL

所以

if ([dictionary valueForKey:@"aString"]==nil)
if ([dictionary valueForKey:@"aString"]==NULL)

两者都检查字典中是否存在特定的键,尽管第一行更正确,因为它检查 Objective-C 类型。

关于:

if ([[dictionary valueForKey:@"aString"] isEqualToString:@""])

此行检查字典中是否存在带有“aString”键的条目,并将该条目与空字符串进行比较。结果将是以下之一:

  • 如果您的密钥没有这样的条目,则为 false
  • 如果您的密钥有该条目并且该条目为空,则为 true 如果
  • 您的密钥的对象存在并且不响应 ,则字符串可能会崩溃 - isEqualToString: message

因此,根据您的需要,您必须使用第一行,或者如果您需要结合检查条目是否存在并且它不是空字符串,那么您需要两个条件:

if ([dictionary valueForKey:@"aString"]==nil ||
               [[dictionary valueForKey:@"aString"] isEqualToString:@""])

nil is NULL's analog for objective-c objects. Actually they're the same:

 //MacTypes.h
 #define nil NULL

So

if ([dictionary valueForKey:@"aString"]==nil)
if ([dictionary valueForKey:@"aString"]==NULL)

both check if the specific key is present in a dictionary, although 1st line is more correct as it checks objective-c types.

About:

if ([[dictionary valueForKey:@"aString"] isEqualToString:@""])

This line checks if there's an entry in dictionary with "aString" key and compares that entry with empty string. The result will be one of the following:

  • is false if there's no such entry for your key
  • is true if there's entry for your key and that entry is empty string
  • may crash if object for your key exists and does not respond to -isEqualToString: message

So depending on your needs you must use 1st line, or if you need to combine both checking if entry exists and it is not an empty string then you need 2 conditions:

if ([dictionary valueForKey:@"aString"]==nil ||
               [[dictionary valueForKey:@"aString"] isEqualToString:@""])
与他有关 2024-10-11 03:37:24
nil == (id) 0
Nil == (Class) 0
NULL == (void *) 0

@"" 是一个空的 NSString 常量。

nil == (id) 0
Nil == (Class) 0
NULL == (void *) 0

@"" is an empty NSString constant.

灰色世界里的红玫瑰 2024-10-11 03:37:24

nil 用于表示指向 Objective-C 对象的空指针。
Nil 用于表示指向 Objective-C 类的空指针。
NULL 用于表示指向其他任何内容的空指针。

所有这些,碰巧都有数值 0。它们都是零,但“NULL”是一个 void *,“nil”是一个 id,“Nil”是一个类指针。

@"" 用于表示空字符串,或者说长度为零的字符串。

nil is used to represent a null pointer to an Objective-C object.
Nil is used to represent a null pointer to an Objective-C class.
NULL is used to represent a null pointer to anything else.

All these, happen to have the numeric value of 0. They're all zero, but "NULL" is a void *, "nil" is an id, and "Nil" is a Class pointer.

@"" is used to denote an empty string, or say a string whose length is ZERO.

故事与诗 2024-10-11 03:37:24

valueForKey:(以及大多数其他 Cocoa 方法)返回一个指向对象的指针(内存地址)。当您使用 == 符号时,您只是测试该指针的数值,而不是字符串的实际值。当您使用 isEqualToString: 时,您正在测试该值。

这两个:

if ([dictionary valueForKey:@"aString"]==nil)
if ([dictionary valueForKey:@"aString"]==NULL)

或多或少是等效的,并且都测试“返回的地址是否等于零?”,这就是 valueForKey: 指示没有找到该键的对象的方式。如果字典确实包含该键的任何对象,无论其值如何(例如,甚至是空字符串),此 if 语句都会返回 false。

(您通常在 Objective-C 中使用 nil 版本,作为样式约定,就像其他任何东西一样)

if ([[dictionary valueForKey:@"aString"] isEqualToString:@""])

测试字符串的实际值以查看它是否是零长度字符串。如果字典中没有对象,它将返回 false,因为当您针对 nil 指针调用 isEqualToString: (或几乎任何其他方法)时,您总是得到零,或假。

valueForKey: (and most other Cocoa methods) returns a pointer (memory address) to an object. When you use the == symbol you're simply test the numeric value of that pointer and not the actual value of the string. When you use isEqualToString:, you are testing the value.

These two:

if ([dictionary valueForKey:@"aString"]==nil)
if ([dictionary valueForKey:@"aString"]==NULL)

are more-or-less equivalent and both test "Was the address returned equal to zero?", which is how valueForKey: indicates there was no object found for that key. If the dictionary does contain any object for that key, regardless of its value (e.g. Even an empty string) this if statement returns false.

(You normally use the nil version in Objective-C, as a styling convention as much as anything else)

This:

if ([[dictionary valueForKey:@"aString"] isEqualToString:@""])

Tests the actual value of the string to see if it's a zero length string. If there is no object in the dictionary, it will return false, since when you call isEqualToString: (or almost any other method) against a nil pointer you always get zero, or false.

梦言归人 2024-10-11 03:37:24

“NSNull 类定义了一个单例对象,在禁止将 nil 作为值的情况下(通常在数组或字典等集合对象中),您可以使用它来表示 null 值。”任何可以使用 null 的地方都可以使用 nil。主要区别在于你可以向nil发送消息,这样你就可以在一些null无法工作的地方使用它。一般情况下,使用nil就可以了。这正是发生的情况,程序员选择将 null 对象放入控制器数组中,其中不允许 nil 作为值。

它更多的是一个上下文引用,其中 NULL 是指向 0x0 的指针,nil 是一个不存在的 Objective-C 对象,Nil 是一个不存在的 Objective-C 类,但从技术上讲,它们都只是 0。而且,它不是 NULL null -- null 在 Java 或 C# 中存在,但在 Objective-C

或你所说的简单方法

中不存在它们都是零,但“NULL”是一个 void *,“nil”是一个 id,“Nil”是一个类指针。

"The NSNull class defines a singleton object you use to represent null values in situations where nil is prohibited as a value (typically in a collection object such as an array or a dictionary)." You can use nil about anywhere you can use null. The main difference is that you can send messages to nil, so you can use it in some places where null cant work. In general, just use nil. that's exactly what's happening, the programmer is choosing to put a null object into the controllers array, where nil is not allowed as a value.

It is more a contextual reference where NULL is a pointer to 0x0, nil is a non-existent objective-c object and Nil is a non-existent objective-c class, but technically they are all just 0. Also, it is NULL not null -- null is in Java or C# but not in Objective-C

or ease ways u say

They're all zero, but "NULL" is a void *, "nil" is an id, and "Nil" is a Class pointer.

寻找一个思念的角度 2024-10-11 03:37:24

NilNULL表示不存在
@""表示存在,为空

Nil and NULL indicate the non-existence
@"" indicates the existence and it's empty

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