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).

12
  • In Objective C, nil is NULL is (a form of) 0 .. Commented Apr 1, 2015 at 21:29
  • The answers in 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. Commented Apr 1, 2015 at 21:31
  • 2
    It's for hysterical reasons. Commented Apr 1, 2015 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. Commented Apr 1, 2015 at 21:46
  • 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 Commented Apr 1, 2015 at 23:35

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.