Are NULL and nil equivalent?

情到浓时终转凉″ 提交于 2019-11-30 08:19:01

nil and NULL are essentially the same, nil is something like (NSObject *)0, while NULL is more like (void *)0. But both are pointers with an integer value of zero. You can send messages to nil without raising an error.

NSNull and NULL (or nil, of course) are different things, however. You just use NSNull as a helper to add an empty object to an NSArray or another container class, since you can't add nil to them. So instead, you use [NSNull null] as a replacement, and you have to check if an array element is NSNull, not if it's nil (it will never be equal to nil).

Rose Perrone

From http://www.iphonedevsdk.com/forum/iphone-sdk-development/34826-nil-vs-null.html

nil and NULL are 100% interchangeable.

From:

  • NULL is for C-style memory pointers.
  • nil is for Objective-C objects.
  • Nil is for Objective-C classes.

Whenever you're writing Objective-C code, use nil Whenever you're writing C code, use NULL

But ultimately they're all defined as the same thing -- (void *)0, I think -- so in practice it doesn't really matter.

The concept is the same, with the difference that it's valid to send messages (call method) to nil.

NSNull is a real (singleton) class, that can be used for arrays or dictionnaries, who don't accept NULL or nil values.

Biggest difference between them: sending a message to an NSNULL object is probably going to cause a crash, whereas it's cool to send any message to nil. For example, if you use a key path to get an array, like so:

NSArray *departmentNames = [departments valueForKey:@"name"];

Then you will have an NSNULL object for any department whose name is nil. So, this is going to cause a crash:

for (NSString *name in departmentNames)
    NSLog(@"%@", [name lowercaseString]);

whenever name is NSNull, because you just sent an unknown selector (lowercaseString) to an NSNull.

Lesson: check for the NSNull object in an array before sending any message to its elements.

for (NSString *name in departmentNames)
    if (name != [NSNull null])
         NSLog(@"%@", [name lowercaseString]);

No, NSNull and nil are not the same. They both represent a lack of value, and you might want to treat them the same, but they are still not equal.

The NSNull object instance represents a null value, for example when you read data from a database that has null values.

The nil value is a null pointer, i.e. it doesn't point to any object instance.

In your second code you don't have any NSNull instance. An NSString pointer that contains a null pointer is not an NSNull instance, it's still just a null pointer. You are comparing one null pointer to another, and they are of course equal.

Make sure you typecast [NSNull null] to object type that you are comparing

NSArray list;
if(list==(NSArray *)[NSNull null])
    // do something

otherwise you will receive a warning message saying "Comparison of distinct pointer types('type *' and 'NSNull *')

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!