Ios 无法访问personnal类的已定义属性

Ios 无法访问personnal类的已定义属性,ios,objective-c,xcode,mkannotation,Ios,Objective C,Xcode,Mkannotation,问题是我有一个主类:MyAnnotation,用于在我的mapView上显示注释 @interface lieuAnnotation : MyAnnotation @property(readonly, nonatomic) UIImage *uneImage; // I cannot access this property. @end 我创建了第二个类lieuAnnotation,从这个类继承了一个新属性(一个UIImage) 请注意,披露指标仅在lieuanotation实例中显示

问题是我有一个主类:
MyAnnotation
,用于在我的mapView上显示注释

@interface lieuAnnotation : MyAnnotation

@property(readonly, nonatomic) UIImage *uneImage; // I cannot access this property.

@end
我创建了第二个类
lieuAnnotation
,从这个类继承了一个新属性(一个
UIImage

请注意,披露指标仅在
lieuanotation
实例中显示

所以
view.annotation
应该是
lieuAnnotation
实例

然后我想访问我的财产:

- (void)detailPinVue:(MKAnnotationView *)view
{
    [aView addSubview:view.annotation.uneImage];
}
事情是我无法访问属性
uneImage
,因为Xcode告诉我:

在“id”类型的对象上找不到属性“uneImage”

但在我看来,这应该是可能的

因此,我也尝试通过这种方式访问它:

lieuAnnotation *anno = [[lieuAnnotation alloc] init];
anno = view.annotation;

[aView addSubview:anno.uneImage];
但它不起作用

感谢您的帮助和建议。

试试:

if ([view.annotation isKindOfClass:[lieuAnnotation class]]) { 
    lieuAnnotation *annotaion = (lieuAnnotation *)view.annotation;
    [aView addSubview:annotation.uneImage];
} else {
    NSLog(@"error %@ / %@", NSStringFromClass([view class]), NSStringFromClass([view.annotation class]));
}

简单回答:您需要在访问属性之前强制转换它(但仅当您100%确定所讨论的对象具有该属性时才执行此操作,否则您将在运行时获得
EXC\u BAD\u访问

说明:所讨论的对象在编译时似乎具有类型
id
id
是ObjC中所有对象的通用类型。并非所有类都具有
uneImage
属性,因此编译器无法判断
id
对象是否具有该属性。编译器认为:“让我们谨慎行事,不要构建”。底线:你比编译器更聪明(就像你现在可能已经做的那样)

修正:


通过
MKMapView addAnnotations:
检查注释的方式。确保您正在添加自定义类的对象

您可以使用
NSLog(@“%@”,view.annotation.class);
了解注释的基类

顺便说一句,你施法的方式是没有必要的。

lieuAnnotation*anno=(lieuAnnotation*)view.annotation;

是正确的方法。

您需要在访问属性之前强制转换它。只需使用:
lieuAnnotation*anno=(lieuAnnotation*)view.annotation;
此外,uneImage是addSubview不接受的UIImage(它需要UIView或UIView子类).@Annakarena你说得对……它一直在崩溃,我可能不聪明……它说什么?如果说
EXC\u BAD\u ACCESS
我就忍不住说“我早就告诉过你了”!:)@Lucien
lieuAnnotation *anno = [[lieuAnnotation alloc] init];
anno = view.annotation;

[aView addSubview:anno.uneImage];
if ([view.annotation isKindOfClass:[lieuAnnotation class]]) { 
    lieuAnnotation *annotaion = (lieuAnnotation *)view.annotation;
    [aView addSubview:annotation.uneImage];
} else {
    NSLog(@"error %@ / %@", NSStringFromClass([view class]), NSStringFromClass([view.annotation class]));
}
- (void)detailPinVue:(MKAnnotationView *)view
{
    [aView addSubview: (lieuAnnotation *)view.annotation.uneImage];
}