In observeValueForKeyPath:ofObject:change:context:
- why do the docs use NULL
instead of nil
when not specifying a context pointer?
5 Answers
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
).
-
3But since the type of
context
inobserveValueForKeyPath:ofObject:change:context:
isvoid *
, doesn't that mean that the data passed as thecontext
could be an object pointer? I would think that to be a common case. That's why I'm confused as to why the docs always useNULL
instead ofnil
. Feb 17, 2009 at 16:38 -
2The type of context: in that method is "void *". "nil" is not a "void *", but NULL is. Feb 17, 2009 at 16:47
-
3You can. void * is any pointer. Nonetheless, you are absolutely right that NULL is the correct constant there. Feb 17, 2009 at 18:16
-
3They said void *. NULL is for void * and nil is for id. Therefore, you pass NULL. If you pass nil, you are lying to your reader, who will think this method takes an id. Feb 17, 2009 at 18:50
-
13Or to think of it another way, NULL is a broader type, and nil is a subset of NULL. In general, use the broadest type you can get away with (ie in Java, write your method to expect a Collection instead of a Vector, unless you need something specific from Vector) Feb 17, 2009 at 18:56
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 *).
-
8Also,
NULL
is differently defined thannil
.nil
is defined as(id)0
.NULL
isn't.– user142019Aug 18, 2011 at 15:40 -
16
-
1That is very interesting. It seems it does not matter than other than for style points. It's like YES/TRUE and NO/FALSE.– BrennanMay 1, 2013 at 19:49
-
1@Brennan, That's not entirely true, just because nil is defined as NULL doesn't mean there's some other hidden implementation behind the scenes. For example, IBAction is defined as void but it has a different meaning when using interface builder when displaying methods to attach to actions on buttons and such. Mar 13, 2015 at 15:49
They're technically the same thing and differ only in style:
- Objective-C style says
nil
is what to use for theid
type (and pointers to objects). - C style says that
NULL
is what you use forvoid *
. - C++ style typically says that you should just use
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.
They almost are the same thing except,
nil
is used in an Objective-C style.
where NULL
is for C type pointers and is typdef'ed to (void *)
.
-
1