Skip to main content

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




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

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?