0

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.

Nick
  • 3,205
  • 9
  • 57
  • 108

1 Answers1

0

You can make a unique string as keypath, and save it in your annotation.

@property(nonatomic) NSMutableDictionary *m_dict;

-(void)addAnnotation:(id<MyAnnotation>)annotation {
    UIView *view = ....;
    NSString *unique_id = ....;
    [annotation setValue:unique_id forKey:@"_unique_id"];
    if (view) {
        [_m_dict setObject:view forKey:unique_id];
    }
    else {
        [_m_dict setObject:[NSNull null] forKey:unique_id];
    }
}

-(UIView *)viewForAnnotation:(id<MyAnnotation>)annotation {
    id ret = [_m_dict objectForKey:annotation valueForKey:@"_unique_id"];
    if ([ret isKindOfClass:[UIView class]]) {
        return ret;
    }
    return nil;
}
eason
  • 2,854
  • 3
  • 16
  • 18