Please help me understand pointers


#1

I’m really taking my time with this book and making sure I understand every concept before continuing on. I thought I had understood the concept of pointers when it was introduced back in Chap 2, but when Chap 5 rolled around I started getting confused again. I don’t understand when to use pointers and when not to. For example:

In Chap 5, Page 114 a local variable is declared in the didUpdateUserLocation method:

CLLocationCoordinate2D loc = [userLocation coordinate];

Is the variable loc not a pointer because we are not creating a new Object here? I thought I understood this, at least somewhat, until the MKAnnotation Class was being constructed on Page 116. In the BNRMapPoint header:

- (id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t;

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

@property (nonatomic, copy) NSString *title;

Why is the title property a pointer and the coordinate property is not? How will I know when to use pointers and when not to? This is the one thing I am kind of stuck on so far…

Thank you!


#2

We don’t use a pointer for CLLocationCoordinate2D because it is a C structure and not an object. “Title” is an NSString object so we do use a pointer.

Use pointers for Objective-C objects. Don’t use pointers for primitive types and structures like int, NSUInteger, CGRect, etc. Use pointers to refer to items in the heap. Don’t use pointers for items that are accessed through the stack.

Xcode also has built-in quick help to identify if something is declared as an object, struct, property, etc. Option-click on the item to bring up the quick help.


#3

That helps a lot. Thank you!

Thanks for the Quick-Help shortcut as well.