NULL vs nil in Objective-C
observeValueForKeyPath:ofObject:change:context: – why do the docs use
NULL instead of
nil when not specifying a context pointer?
- Getting a particular menu item from MainMenu
- How to identify/mark OS X app instance
- How to use UKKQueue in a Cocoa application?
- Writing a privileged helper tool with SMJobBless()
- NSRemoteView generates a strange message in the Log
- How to override a fullscreen game with keywindow borderless window (overlay)?
5 Solutions Collect From Internet About “NULL vs nil in Objective-C”
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
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:
- Objective-C style says
nilis what to use for the
idtype (and pointers to objects).
- C style says that
NULLis what you use for
- C++ style typically says that you should just use
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;
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.
NULL is for C type pointers and is typdef’ed to
- How do I capture the point initially tapped in a UIPanGestureRecognizer?
- Are classes objects in Objective-C?
- Creating a left-arrow button (like UINavigationBar's “back” style) on a UIToolbar
- Swift 4 “This class is not key value coding compliant”
- How to implement video DRM in iOS
- Parse BaaS iOS LoginFields in Swift – .value should be changed to?
- inputAccessoryView Dismiss Keyboard
- XCode breakpoint vs –
- #import using angle brackets < > and quote marks “ ”
- Swift 2.0 code works in Xcode but not in Playground
- looping through an NSMutableDictionary
- Load only five annotations to MKMapVIew
- Correct management of addObserverForName:object:queue:usingBlock:
- App crash on use of PeoplePicker, but not in same view
- Set contentMode of UIImageView