8

Possible Duplicate:
What's the difference between [NSNull null] and nil?
What are the differences between nil, NULL and [NSNULL nil]?

1.

id dicValue = [aDictionary objetForKey:@"aKey"];

if(dicValue != nil)
{
     blablala...
}

or

2.

if(dicValue != [NSNull null]) 
{
     blablala...
}

should I choose the first one,or the second one?

or when it comes like this :

3.

if ([aDictionary objetForKey:@"aKey"] != nil)

or

4.

if ([aDictionary objetForKey:@"aKey"] != [NSNull null])

and what again ?

1.2.3.4. which is correct and recommended?

Community
  • 1
  • 1
Patrick
  • 262
  • 3
  • 12

4 Answers4

18

Directly from Apple Documentation.
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).

NSNull *nullValue = [NSNull null];
NSArray *arrayWithNull = [NSArray arrayWithObject:nullValue];
NSLog(@"arrayWithNull: %@", arrayWithNull);
// output: "arrayWithNull: (<null>)"

It is important to appreciate that the NSNull instance is semantically different from NO or false—these both represent a logical value; the NSNull instance represents the absence of a value. The NSNull instance is semantically equivalent to nil, however it is also important to appreciate that it is not equal to nil. To test for a null object value, you must therefore make a direct object comparison.

id aValue = [arrayWithNull objectAtIndex:0];
if (aValue == nil) {
    NSLog(@"equals nil");
} else if (aValue == [NSNull null]) {
    NSLog(@"equals NSNull instance");
    if ([aValue isEqual:nil]) {
        NSLog(@"isEqual:nil");
    }
}
// output: "equals NSNull instance"

Is it clear now??

Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
3

My understanding is that NSNull is an object representing a NULL value and nil is just an empty pointer.

You can fill a NSArray with NSNull objects and it'll return null values, but you can't fill it with nils.

You can do

[myNsMutableArray addObject:[NSNull null]];

but this would crash:

[myNsMutableArray addObject:nil];

Likewise:

[[NSMutableArray alloc] initWithObjects:@"1", nil, @"2", nil];  
[[NSMutableArray alloc] initWithObjects:@"1", [NSNull null], @"2", nil]; 

The first array, when printed would output "1" The second one would output: "1, null, 2"

So you can use NSNull to represent empty spaces in an array.

You can't insert nil into a dictionary. So checking for it means the key doesn't exist. If you check for NSNull it means that the key exists but holds an empty (NSNull) value.

Odrakir
  • 4,254
  • 1
  • 20
  • 52
1

First one means there is no such key, second means that key's value is NSNull

negersiu
  • 552
  • 3
  • 20
0

In this case, you should use the first one; or rather, a slight variation:

if (dicValue) {
  // blablabla...
}

NSNull is for use when you need a valid object, for example for use as a dictionary key, array entry, or similar.

nil is equal to (id)NULL; it is generally taken to mean "No value".

[NSNull null], on the other hand, is an object holding an empty value; taken to mean "A value of None".

Very few methods return an NSNull value, and it is generally only encountered if you introduce it yourself.

It exists mostly because nil cannot be inserted into arrays; and works as an empty marker for arrays that require it.


On a related note; the "!= nil" part of your expression is redundant; any valid pointer to an object evaluates to true in a boolean expression, and nil evaluates to false.

Williham Totland
  • 28,471
  • 6
  • 52
  • 68
  • 3q,i know it is redundant,but i still like to add the "!= nil" part.it makes the code easy to read !do you think so:) – Patrick Nov 14 '11 at 13:00