Objective-C:NULL、nil 和 @"" 之间有什么区别?
正如标题所说, NULL
、nil
和 @""
之间有什么区别?
例如,如果我想检查字典中的字符串是否为空。
我应该使用哪个条件?
if ([dictionary objectForKey:@"aString"] == nil)
或
if [[dictionary objectForKey:@"aString"] isEqualToString:@""]
或
if ([dictionary objectForKey:@"aString"] == NULL)
哪一个是正确的?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
nil
是 NULL 与 Objective-C 对象的类似物。实际上它们是相同的:所以
两者都检查字典中是否存在特定的键,尽管第一行更正确,因为它检查 Objective-C 类型。
关于:
此行检查字典中是否存在带有“aString”键的条目,并将该条目与空字符串进行比较。结果将是以下之一:
,则字符串可能会崩溃 - isEqualToString:
message因此,根据您的需要,您必须使用第一行,或者如果您需要结合检查条目是否存在并且它不是空字符串,那么您需要两个条件:
nil
is NULL's analog for objective-c objects. Actually they're the same:So
both check if the specific key is present in a dictionary, although 1st line is more correct as it checks objective-c types.
About:
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:
-isEqualToString:
messageSo 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:
@"" 是一个空的 NSString 常量。
@"" is an empty NSString constant.
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.
valueForKey:
(以及大多数其他 Cocoa 方法)返回一个指向对象的指针(内存地址)。当您使用==
符号时,您只是测试该指针的数值,而不是字符串的实际值。当您使用isEqualToString:
时,您正在测试该值。这两个:
或多或少是等效的,并且都测试“返回的地址是否等于零?”,这就是
valueForKey:
指示没有找到该键的对象的方式。如果字典确实包含该键的任何对象,无论其值如何(例如,甚至是空字符串),此 if 语句都会返回 false。(您通常在 Objective-C 中使用
nil
版本,作为样式约定,就像其他任何东西一样):
测试字符串的实际值以查看它是否是零长度字符串。如果字典中没有对象,它将返回 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 useisEqualToString:
, you are testing the value.These two:
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:
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 anil
pointer you always get zero, or false.“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.
Nil和NULL表示不存在
@""表示存在,为空
Nil and NULL indicate the non-existence
@"" indicates the existence and it's empty