[英]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. 我正在尝试遵循在线教程,该教程显示了如何在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: 我有一个'MKPinAnnotationView'的子类,在.m文件中,我创建了这个方法:
- (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. 这是我的“模型”中称为Place.h / .m的部分内容。 Here is where I create the property for 'iconN'. 这是我为“ 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 编辑:这是Place.h和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 还有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
You cannot convert a MKAnnotation
to a Place
just by casting it. 您不能仅通过投射将MKAnnotation
转换为Place
。 This line is wrong. 这行是错误的。
Place *place = (Place *)annotation;
You should post your Place.h
and Place.m
files if you're still stuck. 如果您仍然卡住,则应该发布Place.h
和Place.m
文件。 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. 您需要在新的Place对象上设置iconN属性,或者在Place类中创建一个init方法,该方法接受MKAnnotation
对象作为参数并相应地设置其自己的内部值。
您正在将消息发送到注释,但是您似乎具有注释视图的子类。
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. 在注释变量类(MKPointAnnotation)的变量位置,您不能以这种方式将主类变量带到子类。 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. 相反,您必须从MKPointAnnotation创建Place的构造函数,并在setAnnotation方法中执行检查注释是否为MKPointAnnotation。
Posting as an answer what was originally just a comment: 发布答案,原本只是评论:
I'm not familiar with the MapKit, but the thing that sticks out for me in this: -[MKPointAnnotation iconN]: unrecognized selector sent to instance is that the class is MKPointAnnotation. 我不熟悉的MapKit,而是伸出了我在这个事情: - [MKPointAnnotation iconN]:无法识别的选择发送到实例是类是MKPointAnnotation。 So the annotation you're receiving isn't actually a Place object, it's an MKPointAnnotation object - you can't just cast to Place. 因此您收到的注释实际上不是一个地方的对象,这是一个MKPointAnnotation对象 - 你不能只是投来的地方。 I suspect the root of your problem is where you create your annotation object in the first place. 我怀疑你的问题的根源是创建摆在首位的注释对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.