Friday, February 24, 2012

Unrecognized selector sent to instance?


I have seen that a few others have had this problem as well. I'm trying to follow a tutorial online that shows how to create animated pins on a MapView.



I have implemented the code as shown in the tutorial and the project builds fine except I receive this exception:




-[MKPointAnnotation iconN]: unrecognized selector sent to instance



I have a subclass of 'MKPinAnnotationView' and in the .m file I create this method:




- (void)setAnnotation:(id<MKAnnotation>)annotation {
[super setAnnotation:annotation];

//Place *place = [[Place alloc] init];
Place *place = (Place *)annotation;


//The following line is where the program sends "SIGABRT"
icon = [UIImage imageNamed:[NSString stringWithFormat:@"pin_%d.png", [place.iconN intValue]]];
[iconView setImage:icon];
}



Here are a few parts from my "model" which is called Place.h/.m. Here is where I create the property for 'iconN'.




@property (retain, nonatomic) NSNumber *iconN;



And here I synthesize it:




@synthesize iconN = _iconN;



Any help is greatly appreciated.



EDIT: Here is the Place.h and Place.m




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


@interface Place : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
}

@property (retain, nonatomic) NSNumber *iconN;
@property (nonatomic, copy) NSString *title;
@property (nonatomic) CLLocationCoordinate2D coordinate;

- (id)initWithLong:(CGFloat)lon Lat:(CGFloat)lat iconNumber:(NSNumber *)iconNumber;

@end



And the Place.m




#import "Place.h"

@implementation Place
@synthesize coordinate;
@synthesize iconN = _iconN;
@synthesize title;

- (id)initWithLong:(CGFloat)lon Lat:(CGFloat)lat iconNumber:(NSNumber *)iconNumber {
self = [super init];
if (self) {
coordinate = CLLocationCoordinate2DMake(lat, lon);
self.iconN = iconNumber;
}

return self;
}

- (NSString *)title {
return [NSString stringWithFormat:@"Bus: %d", [self.iconN intValue]];
}

- (NSString *)subtitle {
return [NSString stringWithFormat:@"bus[%d] from database.", [self.iconN intValue] - 1];
}


@end

3 comments:

  1. You cannot convert a MKAnnotation to a Place just by casting it. This line is wrong.

    Place *place = (Place *)annotation;


    You should post your Place.h and Place.m files if you're still stuck. You need to either set the iconN property on a new Place object, or create an init method in the Place class that accepts the MKAnnotation object as a parameter and sets it own internal values accordingly.

    ReplyDelete
  2. In the line

    Place *place = (Place *)annotation;


    has the variable place of annotation variable class (MKPointAnnotation), you are not able to bring the master class variable to a subclass in this way. Instead you'll have to make a constructor for Place from MKPointAnnotation and perform a check in the setAnnotation method that annotation is of MKPointAnnotation.

    ReplyDelete
  3. You are sending the message to the annotation but you seem to have subclasses the annotation view.

    ReplyDelete