In Objective C?
Are they really the same thing?
How to test that an object is nil?
In short they all are 0 and nothing else. The difference is that while NULL represents zero for any pointer, nil is specific to objects (e.g., id) and Nil is specific to class pointers.
Nil is for object pointers, NULL is for non pointers, Null and Nil both defined to be equal to the value zero. NULL is a void *, nil is an id, and Nil is a Class pointer, NULL is used for non-object pointer (like a C pointer) in Objective-C.
Null : invalid or having a value of zero Nill : archaic for refuse (Shakespeare) ☝But I think you were referring to nil spelled with one L. 👇 Nil : zero, nothing, or nonexistent Examples: This contract is null and void. They beat us three to nil.
Null is a specific subtype of a Variant. It has no existence outside of the Variant type, and is created to allow a Variant to model a database null value. Nothing is a value of an Object variable. It essentially is identical to a null pointer, i.e. there is no object.
Nil
and nil
are defined to be the same thing (__DARWIN_NULL
), and are meant to signify nullity (a null pointer, like NULL
). Nil
is meant for class pointers, and nil
is meant for object pointers (you can read more on it in objc.h; search for Nil
). Finally, you can test for a null value like this:
if (object == nil)
or like this:
if (!object)
since boolean evaluations will make any valid pointer that contains an object evaluate to true.
nil
is the Objective-C constant for a null pointer to an object, Nil
is identically defined. In practice, it is has the same value as the C constant NULL
. Test for a nil object like this:
if (fooObj == nil)
In my code, I tend to use nil
for Objective-C objects and NULL
for other C pointers. This is a matter of personal taste - currently nil and NULL are interchangeable for comparison in all existing Objective-C implementations.
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