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
).
They're technically the same thing (0), but nil is usually used for an Objective-C object type, while NULL is used for c-style pointers (void *).
They're technically the same thing and differ only in style:
nil
is what to use for the id
type (and pointers to objects).NULL
is what you use for void *
.0
.I typically use the variant that matches the language where the type is declared.
NULL
is the C equivalent
of nil
, a pointer to nothing;
where nil is zero typed as id
,
NULL is zero typed as void*
.
One important point you can’t send a message to NULL. So it is preferred to use nil in objective-C at many places.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With