I had a question regarding the use of asterisks in Objective-C. Just to be clear: I understand what pointers are and everything in procedural C. I was wondering two things though:
1) Why are all (references to) Objective-C objects pointers? Why not plain variables? (i.e. NSArray array = [[NSArray alloc] init];)
2) Why do you omit the asterisk when calling method?
Thanks!
Think of an Objective-C object as a glorified struct for a moment.
NSArray array;in a local scope would be an object “allocated” on the stack.NSArray *array;indicates an object backed by a hunk of memory, typically allocated from the heap.Long, long, ago it was decided that Objective-C wouldn’t support objects on the stack. Mostly, because doing so would make memory management a gigantic mess (mainly,
-retainof something on the stack doesn’t make any sense at all).Since Objective-C was designed as a pure superset of C and, unlike C++, doesn’t try to modify the basic behavior of C, having the
*in there seemed natural.Note that
idbreaks this, butidis also a completely generic type. And it is, in fact, possible to have Objective-C objects on the stack. In Snow Leopard, Blocks are actually Objective-C objects and they do start on the stack. It isn’t, however, supported for you to create your own objects on the stack.Because you aren’t dereferencing the pointer to the object and that pointer to the object is critical within the method implementation itself. When you say…
… it is exactly equivalent to writing …
… and when you implement said method ….
… it is exactly equivalent to implementing this C function …
That is, an Objective-C method is really just a C function that takes two or more arguments. As a matter of fact, you can paste this line of code into any method implementation and it’ll “just work”: