As the title says, what\'s the difference between NULL
, nil
and @\"\"
?
For example, if I want to check a string in a dictiona
Nil and NULL indicate the non-existence
@"" indicates the existence and it's empty
nil == (id) 0
Nil == (Class) 0
NULL == (void *) 0
@"" is an empty NSString constant.
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.
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:
-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:
if ([dictionary valueForKey:@"aString"]==nil ||
[[dictionary valueForKey:@"aString"] isEqualToString:@""])
"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 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.