views:

44

answers:

2

Hello world..

I have been playing with objective C a little and am finding it a great language..

Coming from C# i found pointers a little hard but now i understand the concept and how to use them..

ie:

MyObject* x = [[myObject alloc] callinitializer];

which create a new object on the heap and a pointer on the stack..

but can somebody please explain why to access the objects properties you do this:

[x setsomeprop: @"I Like Pizza"];

instead of this:

[*x setsomeprop: @"With Pineapple"];

without the dereferencing star arent we just working with the pointer instead of the object itself??

Confuesd!

Thanks

Daniel

+5  A: 

No. The bracket syntax is a language feature specifically for objects - it dereferences the pointer automatically.

Sam Dufel
Thanks a lot thats awesome!.. i've been tearing my hair out thinking i was stupid cause not one of the obj-c books i've read said that i just shrugged and assumed that was happening, Oh btw did i get the stack / heap terminology right?
Daniel Upton
Yep, you got it.
Sam Dufel
A: 

Partly this is just a result of how method dispatch works in a dynamic language like Objective-C. There's almost nothing useful you can do with a dereferenced object pointer in Objective-C.

Given that all objects are stored on the heap, and manage their own lifecycle with retain/release (or via garbage collection), a pointer to the object is exactly what you want to use in 99% of all situations.

As it turns out, essentially the same mechanism is used in C# and Java - object references are pointers, which is why assigning one reference to another makes them point at the same object, rather than copying the object.

Mark Bessey