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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.