In observeValueForKeyPath:ofObject:change:context: - why do the docs use NULL instead of nil when not specifying a context pointer?
-
in my opinion, object is nil, class is Nil, and NULL using for object or classThanh Vũ Trần– Thanh Vũ Trần2012-11-09 01:45:23 +00:00Commented Nov 9, 2012 at 1:45
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).
14 Comments
context in observeValueForKeyPath:ofObject:change:context: is void *, doesn't that mean that the data passed as the context 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 use NULL instead of nil.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 *).
4 Comments
They're technically the same thing and differ only in style:
- Objective-C style says
nilis what to use for theidtype (and pointers to objects). - C style says that
NULLis 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.
Comments
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 *).