Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Red87445

macrumors newbie
Original poster
Jul 1, 2011
7
0
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!
 
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.
 
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.
 
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 (<<-- click)
and
2. Wiktionary definition of 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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.