I have a bunch of MKPolygon
saved in an array, but I wanted to save more data than the MKPolygon class has storage for (more specifically: a notes field that describes what that polygon is). Subclassing is, for some reason, not possible, so I found the associative objects feature that is, for my purposes, good enough. This is what my .m looks like:
@implementation MKPolygon (ExtraProperties)
static char notesKey;
- (void)setNotes:(NSString *)notes {
objc_setAssociatedObject(self, ¬esKey, notes, 1);
}
- (NSString *)notes {
return objc_getAssociatedObject(self, ¬esKey);
}
My .h just declares a property for this category:
@interface MKPolygon (ExtraProperties)
@property (nonatomic, retain) NSString *notes;
@end
Setting the notes property works well, no errors and it appears to store the variable. Getting it, on the other hand, works, but gives me a warning that -notes wasn't found in that protocol. It's probably important to let you know that the overlay is also added as annotation, and that I get this '-notes not found in protocol' error in my mapView:viewForAnnotation:
method, for which the second argument is an id
that conforms to the MKAnnotation
protocol.
Now, I understand that it says that the MKAnnotation
protocol doesn't implement the -notes method, but I n开发者_开发百科ever claim it does. I've created a category with two extra methods (getter/setter), so if my understanding of categories is correct, -notes is now considered to be a method of MKPolygon, correct? Then why is the compiler giving me this warning?
Also noteworthy: calling the method does actually work: I get the correct results, but I don't like having warnings in my code.
Since it is an MKAnnotation error.. Try implimenting it in your id... something like this probably..
id<MKAnnotation,NSObject> delegate;
"MKAnnotation" might be the wrong thing to put, you'd have to look it up in the xcode library but that's my best guess... good luck if it works, good-er luck if it doesn't /: haha
精彩评论