Search code examples
iphoneiosios6mapkitmkannotation

Mapkit addAnnotation exception: unrecognized selector sent to instance


My code relevant to the problem:

Annotation .h file

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>   

@interface CityAnnotation : NSObject <MKAnnotation> {
    NSString *name;
    NSString *country;
    CLLocationCoordinate2D coords;
}

@property (nonatomic,copy) NSString *name;
@property (nonatomic,copy) NSString  *country;
@property (nonatomic,assign) CLLocationCoordinate2D coords;

-(id)initWithTitle:(NSString *)cityname AndCountry:(NSString *)countryname AndCoords: (CLLocationCoordinate2D)coordinate;


@end

Annotation .m file

#import "CityAnnotation.h"

@implementation CityAnnotation

@synthesize name,country,coords;

-(id)initWithTitle:(NSString *)cityname AndCountry:(NSString *)countryname AndCoords:(CLLocationCoordinate2D)coordinate
{
    self  = [super init];
    if (self){
    self.name = cityname;
    self.country = countryname;
    self.coords = coordinate;
    }
    return self;

}

@end

My imports into my mapview:

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "tableViewController.h"
#import "City.h"
#import "CityAnnotation.h"

The adding of the Annotation in the View Controller. This is where the error happens on [self.mapView addAnnotation:newAnnotation];

- (void)viewDidLoad
{
    [super viewDidLoad];
    CityAnnotation *newAnnotation = [[CityAnnotation alloc]initWithTitle:self.SelectedCity.name AndCountry:self.SelectedCity.country AndCoords:self.SelectedCity.location];
    [self.mapView addAnnotation:newAnnotation];


}

I am getting this error:

-[CityAnnotation coordinate]: unrecognized selector sent to instance 0x85c7d70
2013-06-17 12:15:57.694 JsonTest[1769:c07] *** Terminating app due to uncaught exception     'NSInvalidArgumentException', reason: '-[CityAnnotation coordinate]: unrecognized selector     sent to instance 0x85c7d70'

Solution

  • Your CityAnnotation class has to provide a property/method named coordinate. See the MKAnnotation Protocol Reference for details.

    You may be able to fix your problem by just renaming your coords property to coordinate.