代码之家  ›  专栏  ›  技术社区  ›  Henrik Erlandsson

自定义批注,“访问未知getter方法”

  •  0
  • Henrik Erlandsson  · 技术社区  · 15 年前

    在MapLocation.h和.m中多次尝试设置自定义getter和setter之后,我将其归结为无法复制(必需的)getter title,将其重命名为title2,并尝试获取其返回值。这是我的密码:

    -(MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation {
    static NSString *placemarkIdentifier=@"Map Location Identifier";
    NSString *str1=annotation.title;
    NSString *str2=annotation.title2;
    if([annotation isKindOfClass:[MapLocation class]]) {
        MKAnnotationView *annotationView=(MKAnnotationView *)[theMapView dequeueReusableAnnotationViewWithIdentifier:placemarkIdentifier];
        if (annotationView==nil) {
            annotationView=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:placemarkIdentifier];
        }
        else
            annotationView.annotation=annotation;
    
    
        return annotationView;
    }
    return nil;
    

    }

    1 回复  |  直到 15 年前
        1
  •  3
  •   deanWombourne    15 年前

    尝试将线从点符号改为:

    NSString *str2=[annotation title2];
    

    错误应该会消失。

    MKAnnotation . 事实上,你知道它还有什么其他的方法是无关紧要的;编译器不是通灵的-它只知道注释遵循MKAnnotation协议,仅此而已。它看到title getter的原因是因为title是在MKAnnotation中定义的。

    您还可以通过使用强制转换来修复此问题:

    MapLocation *mapLocation = (MapLocation *)annotation;
    

    现在,你可以说

    NSString *str2=mapLocation.title2;
    

    因为您已经告诉编译器mapLocation是mapLocation对象。