2010年5月27日 星期四

nil vs Nil vs Null vs NSNull null

nil: A null pointer to an Objective-C object.
(#define nil ((id)0))

Nil: A null pointer to an Objective-C class. 

Null: A null pointer to anything else.
(#define NULL ((void*)0))

NSNull: A class defines a singleton object used to represent null values in collection objects (which don’t allow nil values).

[NSNull null]: The singleton instance of NSNull.


ex1:

[NSApp beginSheet:sheet
        
modalForWindow:mainWindow
        
modalDelegate:nil //pointing to an object
        
didEndSelector:NULL //pointing to a non object/class
        
contextInfo:NULL]; //pointing to a non object/class


ex2:

NSObject *obj1 = [NSObject new];
NSObject *obj2 = nil;
NSObject *obj3 = [NSObject new];
NSArray *values1 = [NSArray arrayWithObjects:obj1, obj2, obj3, nil]);

The values1 will only have obj1.


NSObject *obj1 = [NSObject new];
NSObject *obj2 = [NSNull null];
NSObject *obj3 = [NSObject new];
NSArray *values2 = [NSArray arrayWithObjects:obj1, obj2, obj3, nil]);

The values2 will have 3 objects and the second object is the instance of NSNull.

-----------
Reference:
1. http://numbergrinder.com/node/49
2. http://forums.macrumors.com/archive/index.php/t-128519.html%253C/t-522591.html
3. http://lists.apple.com/archives/cocoa-dev/2001/May/msg00704.html

1 則留言: