MyAnnotation没有实现MKAnnotation协议
-
19-09-2019 - |
题
我有警告“MyAnnotation不实现MKAnnotation协议”每次我使用:
[mapView addAnnotation:annotation];
or
[mapView removeAnnotation:mapView.annotations];
有人有一个想法?
解决方案
(假设你注释对象是MyAnnotation类的实例)
的MKMapView要求其注释对象符合MKAnnotation
协议,以确保他们实现某些必要的方法 - 否则你的应用程序可以产生运行时错误。此协议定义如下:
// MKAnnotation.h
@protocol MKAnnotation <NSObject>
// Center latitude and longitude of the annotion view.
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@optional
// Title and subtitle for use by selection UI.
- (NSString *)title;
- (NSString *)subtitle;
@end
这是您的MyAnnotation类必须定义和实现coordinate
属性,并且也可以实现2种任选title
方法。为了让编译器知道你的类实际上符合你必须声明你的类以下方式协议:
@interface MyAnnotation: NSObject <MKAnnotation> // Or whatever parent class you have
不隶属于 StackOverflow