Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

some confusions about [NSNull null],nil [duplicate]

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?

like image 964
Patrick Avatar asked Nov 14 '11 12:11

Patrick


People also ask

What is difference between NULL and nil?

Nil is for object pointers, NULL is for non pointers, Null and Nil both defined to be equal to the value zero. NULL is a void *, nil is an id, and Nil is a Class pointer, NULL is used for non-object pointer (like a C pointer) in Objective-C. Like nil , NULL got no value nor address (used to check if a struct is empty).

What is NSNull?

A singleton object used to represent null values in collection objects that don't allow nil values.

How do you write NULL in Swift?

NULL has no equivalent in Swift. nil is also called nil in Swift. Nil has no equivalent in Swift. [NSNull null] can be accessed in Swift as NSNull()


3 Answers

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??

like image 127
Parag Bafna Avatar answered Oct 12 '22 17:10

Parag Bafna


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.

like image 37
Odrakir Avatar answered Oct 12 '22 16:10

Odrakir


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

like image 32
negersiu Avatar answered Oct 12 '22 18:10

negersiu