PDA

View Full Version : What is nil?




Red87445
Jul 27, 2011, 01:18 PM
Objective-C is my first programming language, so I am not familiar with nils or nulls and such. In my book the code created "id action", and assigned it equal to nil. After that there was a switch case where action gets assigned a value. I was just wondering why action was set to nil? Is it just something you do with ids?

Thank you!



jiminaus
Jul 27, 2011, 05:36 PM
An id variable is a pointer to an Objective-C object. nil is the value of an id variable that doesn't currently point to any Objective-C object. NULL is similar except NULL has the type void* not id, and is the value of any kind of pointer variable which doesn't current point to anything.

I think almost always (if not always), nil == NULL == 0.

Mr. Retrofire
Jul 27, 2011, 06:15 PM
What is nil?

An interesting question!

I have two answers for you:
1. Names for the number 0 in English (http://en.wikipedia.org/wiki/Names_for_the_number_0_in_English) (<<-- click)
and
2. Wiktionary definition of nil (http://en.wiktionary.org/wiki/nil) (<<-- click)

:eek:

Sydde
Jul 27, 2011, 11:36 PM
In my book the code created "id action", and assigned it equal to nil.

You have a poorly written book, designed to lead to later confusion. They should not be defining a variable named "action" of type "id", that is just plain wrong. "action" should really only be used as a property, of type "SEL", a sort of AppKit convention. Get a better book.

Red87445
Jul 29, 2011, 02:40 PM
An id variable is a pointer to an Objective-C object. nil is the value of an id variable that doesn't currently point to any Objective-C object. NULL is similar except NULL has the type void* not id, and is the value of any kind of pointer variable which doesn't current point to anything.

I think almost always (if not always), nil == NULL == 0.

thanks!

An interesting question!

I have two answers for you:
1. Names for the number 0 in English (http://en.wikipedia.org/wiki/Names_for_the_number_0_in_English) (<<-- click)
and
2. Wiktionary definition of nil (http://en.wiktionary.org/wiki/nil) (<<-- click)

:eek:

Um, thanks too.

You have a poorly written book, designed to lead to later confusion. They should not be defining a variable named "action" of type "id", that is just plain wrong. "action" should really only be used as a property, of type "SEL", a sort of AppKit convention. Get a better book.

Well, this is a cocos2d book. The reason why I asked a question here and not on the cocos2d forum was because I thought I would get a faster reply, and this wasn't a cocos2d specific question.