I need to realize an interface that mimics MKMapView
in some sense. Specifically, I need to support methods
-(void)addAnnotation:(id<MyAnnotation>)annotation;
-(UIView *)viewForAnnotation:(id<MyAnnotation>)annotation;
Internally there is a mapping which maps an annotation to an UIView
object (which may be null).
So how should I store these annotations and corresponding views? The natural choice would be NSMutableDictionary
:
@property(nonatomic) NSMutableDictionary *m_dict;
-(void)addAnnotation:(id<MyAnnotation>)annotation {
UIView *view = ....;
if (view) {
[_m_dict setObject:view forKey:annotation];
}
else {
[_m_dict setObject:[NSNull null] forKey:annotation];
}
}
-(UIView *)viewForAnnotation:(id<MyAnnotation>)annotation {
id ret = [_m_dict objectForKey:annotation];
if ([ret isKindOfClass:[UIView class]]) {
return ret;
}
return nil;
}
But it does not work, because there is no guarantee that id<MyAnnotation>
conforms NSCopying
.
I thought about saving id<MyAnnotation>
to a raw pointer and then storing the pointer into NSNumber
(which conforms NSCopying
). But I am not sure is this a good idea and how to implement it safely.