Enumeration - unrecognized selector sent to instance
i try to access my enumeration but it doesnt work!!!
i make a typedef enum in my Annotation.h and i try in another class to access one element of the enum...
typedef enum
{
AnnotationTypeMale = 0,
AnnotationTypeFemale = 1
} AnnotationType;
@interface Annotation : NSObject <MKAnnotation>
{
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
AnnotationType annotation_type;
}
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic,retain) NSString *title;
@property (nonatomic,retain) NSString *subtitle;
@pr开发者_开发技巧operty (nonatomic,getter=getAnnotationType,setter=setAnnotationType) AnnotationType annotation_type;
@end
this was my Annotation.h and in my Annotation.m i synthesize all and i included Annotation.h also... in my other class i try now to access AnnotationType...
- (AnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation
{
AnnotationView *annotationView = nil;
// determine the type of annotation, and produce the correct type of annotation view for it.
Annotation* myAnnotation = (Annotation *)annotation;
if([myAnnotation getAnnotationType] == AnnotationTypeMale)
{
the if statement doesnt work..this error occurs: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MKUserLocation getAnnotationType]: unrecognized selector sent to instance 0x5c43850'
any solutions?????? thx
The error says [MKUserLocation getAnnotationType]: unrecognized selector...
. This means the viewForAnnotation method is trying to call getAnnotationType on an annotation of type MKUserLocation.
In your map view, showsUserLocation must be set to YES which means the map is adding its own blue dot annotation (of type MKUserLocation
) for the user's location in addition to the annotations of type Annotation
you are adding.
In viewForAnnotation, you need to check what type of annotation it is before trying to treat it like your Annotation
. Since you're not checking, the code tries to call getAnnotationType on every type of annotation regardless of type but MKUserLocation doesn't have such a method so you get the exception.
You can either check if the annotation is of type MKUserLocation and immediately return nil:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
//your existing code...
}
or check if the annotation is of type Annotation and execute that specific code:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
MKAnnotationView *annotationView = nil;
if ([annotation isKindOfClass:[Annotation class]])
{
// determine the type of annotation, and produce the correct type of annotation view for it.
Annotation* myAnnotation = (Annotation *)annotation;
if([myAnnotation getAnnotationType] == AnnotationTypeMale)
{
//do something...
}
else
//do something else…
}
return annotationView;
}
精彩评论