Pointers on Objective-c

dereference, nsstring, objective-c, pointers

Solution

All the usage of objects in objective c is done through object references, e.g. pointers.

- The Objective C syntax lets you treat objects without dereferencing them, this is different than C / C++ for that matter.

- `str` is an object reference of type `NSString` (as `myFraction`) and `@"Programming can be a headache"` is an object reference of type `NSString` as well, so you can assign it to `str`.

Problem

From what I understand (and please correct me if I'm wrong): ``` int x, count = 10; int *hello; hello = &count; x = *hello; ``` Here the variables x and count are declared to be of type integer. Additionally, the variable count is assigned the value of 10. hello is a pointer to type integer. hello is then assigned the address of count. In order to access the value of count, hello must have an asterisk in front of it, ie, *hello. So, x is assigned the value of whatever is in count and in this case, 10. However... ``` Fraction *myFraction = [[Fraction alloc] init]; [myFraction someMethod]; ``` Here, if I understand correctly, myFraction is a pointer to an instance of Fraction class. myFraction is pointing to (or rather assigned the address of) an object which has been assigned memory and initialised. Surely, in order to access the object that myFraction points to, I ought to write: ``` [*myFraction someMethod]; ``` Given the way in which x accessed the value of count, surely in order to access the object, one ought to write this and not: ``` [myFraction someMethod]; ``` In addition, if I have ``` NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSString *str = @"Programming can be a headache"; NSLog (@"%@\n", str); ``` Why is str being treated as an object above? Or is str an object and in which case, why would I want to make it point to an instance of NSString class? Surely, I ought to be able to just assign an object to str?

Original source