Possible Duplicate:
objective c difference between id and void *
why most of the objects we create in iphone are pointers
According to Stanford university course, 2010/2011
Lecture 3
The guy made something strange there (at least for me), which is that
NSString *digit = sender.titlelabel.text;
Why is digit a pointer?
The type of your
digitisid, which is just basically just a C pointer to a certain struct. All references to objects in Objective-C have this primitive type, regardless of the Class of the object. So the answer to your question is, unfortunately, because that’s the way Objective-C works.So whether you’re declaring an
NSString*, or anUITableViewController*, orMyClass*, your variable has typeid. This is the primary means by which the language implements polymorphism. So, for example, the following declarations are equivalent:And it’s true of method prototypes as well. These are equivalent:
A variable of type
idis not an object itself, it is a pointer to an object. It is the handle with which you manipulate an object. Objective-C does all of the class compatibility work at runtime.Hope this helps. Any questions?
Updates
That’s right:
int,float,double,char,void, and the pointer combinations, are all C primitive types. You can and will still use these quite a bit, and they are just what they are in a C program. But Objective-C adds theidtype as a way to bridge the gap between the primitive typing of C and the very high-level typing of objects by the Objective-C system.iditself is typedef’d as a pointer to a simple struct inobjc.h. At the level of the compiler and the language itself, there really isn’t too much meaning to the type. For example, you’ll almost never declare an array ofids, certainly never perform any arithmetic with them.In fact, it’s not too far a stretch to say that Objective-C is just plain vanilla C with some added syntax (particularly, the square-bracket notation for method invocation), a few extra primitive types (
id,IMP,SEL), and a big runtime library. It’s this runtime library that handles all things Object-Oriented.Anyway, to answer your question, when you’re actually programming, you will most often (99% of the time) just use class names to declare your variables –
NSString *,NSData *,UITableViewController *, and so on. And the compiler will know what you’re talking about, and issue a warning if you write code that clearly tries to put anNSString*where anNSData*is expected. But the actual meaning of those types really exists only at runtime.I’ve digressed a little, but I’m not sure where your understanding is failing you, so I thought I’d just explain things a bit. You might want to read Apple’s The Objective-C Programming Language to get a feel for the language.