NULL和nil在Objective-C

NULL和nil在Objective-C

问题描述:

observeValueForKeyPath:ofObject:change:context: - 为什么文档使用 NULL 而不是 nil 当不指定上下文指针时?

In observeValueForKeyPath:ofObject:change:context: - why do the docs use NULL instead of nil when not specifying a context pointer?

应该仅用于代替 id ,我们的Java和C ++程序员会将其视为指向对象的指针。使用 NULL 作为非对象指针。

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.

查看该方法的声明:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
    change:(NSDictionary *)change context:(void *)context

上下文是 void * (即C风格指针),所以你肯定会使用 NULL (有时被声明为(void *)0 )而不是 nil (类型 id )。

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).