0

I'm new to Objective-C & SO, so bear with me - I've been reading some questions on when to use nil, NULL, or Nil.

My question is: Why do we need so many types of "null"? Can't Xcode (or a similar IDE) interpret what "null" is necessary for the situation?

To clarify, I'm not looking for when to use each - this is clearly explained. For example, why can't Objective-C simply define NULL as nothing, and then just use context to figure out which nothing you needed. I'm quite positive the answer will either be for historical, or necessary purposes (with an example please), or both.

e.g.

NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionary];
mutableDictionary[@"someKey"] = NULL;

... in this case, the compiler would understand you need a "null" object inserted, & simply add this behind the scenes so the dictionary would behave intuitively (& not just cause an error).

Sitric
  • 675
  • 1
  • 4
  • 15
  • In Objective C, nil *is* NULL *is* (a form of) 0 .. – user2864740 Apr 01 '15 at 21:29
  • The answers in http://stackoverflow.com/questions/557582/null-vs-nil-in-objective-c explain when to use - from a contractual/semantic point of view - the different forms and the rationale behind such choices. – user2864740 Apr 01 '15 at 21:31
  • 2
    It's for hysterical reasons. – Hot Licks Apr 01 '15 at 21:40
  • Just a side-note: Messages can be sent to nil without crashing the program, but any messages sent to NULL will crash the program. – Zane Helton Apr 01 '15 at 21:46
  • That's not correct, @ZaneHelton; `nil == NULL`. http://stackoverflow.com/a/18562946/ – jscs Apr 01 '15 at 22:04
  • @JoshCaswell That's not correct, NULL is actually a void pointer (void *) while nil is an id. They're not the same thing. – Zane Helton Apr 01 '15 at 23:26
  • 2
    @ZaneHelton, semantically they are not the same, but they are identical in value and will function interchangeably. You will not cause a crash by sending a message to `NULL` because `nil` _is_ `(id)NULL`. Look at your objc.h, and then types.h – jscs Apr 01 '15 at 23:35
  • this has relatively nothing to do with my question, but I'd like to say thanks for all the quick attention & responses! despite the 0 vote count, the attention from experts makes me enticed to keep using this resource (appropriately) :) – Sitric Apr 02 '15 at 04:40
  • I guess you're right @JoshCaswell. I've been constantly told that sending a message to NULL will crash your program but after try after try it won't crash! – Zane Helton Apr 02 '15 at 10:18
  • Perhaps the people telling you that are Java programmers, @ZaneHelton? – jscs Apr 02 '15 at 18:13
  • @JoshCaswell Usually just in tutorials and whatnot; I'd always taken their word for it. Thanks for making find out for myself! – Zane Helton Apr 02 '15 at 18:21
  • Kudos for checking it out on your own, @ZaneHelton. – jscs Apr 02 '15 at 18:23

1 Answers1

1

there is an NSHipster Article covering the different types of null

Basically, NULL is the null value for C-pointers,
nil is the null value for Objective-C-objects,
Nil is the null value for Objective-C-classes and
[NSNull null] is an Objective-C-Object to represent null.

Palle
  • 11,511
  • 2
  • 40
  • 61