Tuesday, September 28, 2010

Note about MKAnnotation Protocol implementation

Sometimes you just have to take things as they are given, just like that. For example adding MKAnnotation protocol support all you have to do is create an instance variable called "coordinate":
@interface MyPointItem : NSObject <MKAnnotation> {
    NSString *title;
    NSInteger distance;
    CLLocationCoordinate2D coordinate;
}
As Apple documentation says:
"An object that adopts this protocol must implement the coordinate property."
There you are. As long as you remember, WHY you have a variable called "coordinate" INSTEAD OF "location". If you don't remember and apply Natural Naming to your code, your application will start crashing horribly for mysterious reasons...

2 comments:

  1. While you have to use that very generic name for the property you can still have a descriptive name vor your ivar (had to replace the angled brackets with []):

    @interface MyPointItem : NSObject [MKAnnotation] {
    CLLocationCoordinate2D myReallyWellNamedLocation;
    }
    @property (nonatomic, assign) CLLocationCoordinate2D coordinate;
    @end

    @implementation MyPointItem
    @synthsize coordinate=myReallyWellNamedLocation;
    @end

    ReplyDelete
  2. Already put it behind custom initWithId:Andlocation but this is definitely good to remember! Didn't know such is possible!

    Hmph, auto code-complete still shows me coordinate instead of myReallyWellNamedLocation... but at least interface now matches data in protocol packets. Thanx!

    ReplyDelete