nil只能被应用在id类型的地方,就是Java与C++中指向对象的指针。而NULL用于非对象指针。
请看如下实例:
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context
形参context是空指针类型,是一个C类型指针,所以用NULL定义,空指针类型有时被声明为
(void *)0 ,而不是 id类型的nil.
原文:
"nil" should only be used in place of an "id", what we Java and C++ programmers would think of as a pointer to an object. Use NULL for non-object pointers.
Look at the declaration of that method:
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context
context is a "void *" (ie a C-style pointer), so you'd definitely use NULL (which is sometimes declared as "(void *)0") rather than nil (which is of type "id").