Iphone MKMapView-删除注释会导致应用程序崩溃

Iphone MKMapView-删除注释会导致应用程序崩溃,iphone,ios,xcode,cocoa-touch,ios5,Iphone,Ios,Xcode,Cocoa Touch,Ios5,以以下方式从“我的地图视图”中删除注释: if ([[self.mapView annotations] count] > 0) { [self.mapView removeAnnotations:[self.mapView annotations]]; } CLLocationCoordinate2D pinPosition; for (int index = 0; index < [array count]; index++) { Stati

以以下方式从“我的地图视图”中删除注释:

 if ([[self.mapView annotations] count] > 0)
{
    [self.mapView removeAnnotations:[self.mapView annotations]];
}
 CLLocationCoordinate2D pinPosition;
for (int index = 0; index < [array count]; index++)
{        
    Station *aStation = [array objectAtIndex:index];
    PFAnnotation *stationPin = [[PFAnnotation alloc] init]; //StationPinView
    pinPosition = CLLocationCoordinate2DMake([[aStation valueForKey:@"latitude"] doubleValue], [[aStation valueForKey:@"longitude"] doubleValue]);
    stationPin.stationName = [aStation valueForKey:@"stationName"];
    stationPin.stationPosition = pinPosition;
    stationPin.stationLength = [aStation valueForKey:@"platformLength"];

    [self.mapView addAnnotation:stationPin];
    [stationPin release];        


}
导致我的应用程序崩溃,出现以下异常:

*** Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer <MKAnnotationContainerView 0xe87b420> for the key path "title" from <PFAnnotation 0x10851230> because it is not registered as an observer.'

我在其他一些线程中读到,从后台线程设置注释属性是导致上述错误的原因。但在我的例子中,情况并非如此,因为每件事情都是在主线程上执行的。请告知。

在PFAnnotation类中,您是否声明了协议中的标题和副标题属性


如果您的PFAnnotation确实对字符串值使用了不正确的setters-getter:

从这里开始:

塞特:

- (void) setCaption: (NSString*)input
{
    [caption autorelease];
    caption = [input retain];
}
吸气剂:

- (NSString*) caption 
{
    return caption;
}
发布:

- (void) dealloc
{
    [caption release];
    [super dealloc];
}
此外,以这种方式提供坐标更容易:(也适用于ios 3.1.3)

超过(仅适用于ios 4)


请检查是否在代码中的任何地方对属性“title”进行了显式删除

好的..终于解决了!!!我认为这是因为在添加注释时提供了动画。由于有许多注释是与动画背靠背添加的,并且在动画开始之前注释被删除,因此可能存在对已发布注释的引用(这是我的猜测)。此外,删除+添加过程是在每个RegionIDChangeAnimated调用上进行的,这可能会在删除和添加过程之间产生重叠。无论如何,我是如何解决这个问题的,我提供了一个计时器,它将在每个RegiondChangeAnimated后仅在1秒后启动,以确保用户已完成拖动。这样就避免了不必要的添加和删除注释,我也避免了崩溃。感谢所有在这里花时间支持我的人,特别是Guntis Treuland。

请先添加一些代码,说明如何添加注释。!谢谢@Guntis Treuland我编辑了问题并添加了代码。请看。如果不设置stationName和stationLength,会发生什么情况?它还会崩溃吗?如果我不设置stationName,它似乎不会崩溃。(从我目前的测试来看)这很重要。决不应将retain用于NSString属性。NSArray、NSData、NSDictionary或任何其他具有可变版本的类也是如此。查看此处了解原因:我在问题中添加了类文件。它们符合苹果的要求吗?(对不起,我不是一个专业的开发人员)。我不明白你为什么要覆盖title/subtitle的getter…你应该声明属性并合成它们;后来,当您创建注释时,只需设置它们,因为我是维护人员,我不确定最初的开发人员为什么这样写:)。让我试试您指定的路线。我是否需要创建与MKAnnotation协议声明中的名称相同的属性-“title”、“subtitle”和“coordinate”?是的..但这似乎也没有帮助:(.您可以尝试实现这些mapPin类文件吗?希望这能解决问题。否:(…我已经实现了链接中指定的类文件,但问题仍然存在..好的,那么问题显然不在PFAnnotation中,因为,当我在我的项目中实现PFAnnotation时,它在删除注释时没有崩溃。你能在使用注释的地方发布一些代码吗?好的,那么问题不在PFAnnotation中毕竟,我在我的项目中也实现了PFAnnotation,但它没有崩溃。那么,您是否可以显示使用map Anotation的代码?例如,在这些函数中会发生什么:?-(MKAnnotationView*)mapView:(MKMapView*)mapView视图for注释:(id)注释-(void)mapView:(MKMapView*)mp annotationView:(MKAnnotationView*))视图标注访问控制点击:(UIControl*)控制-(无效)地图视图:(MKMapView*)地图视图未选择注释视图:(REVClusterAnnotationView*)视图,还请通过显式调用“performSelectorOnMainthread”尝试在主线程中强制删除和添加注释。
- (NSString*) caption 
{
    return caption;
}
- (void) dealloc
{
    [caption release];
    [super dealloc];
}
stationPin.stationPosition = (CLLocationCoordinate2D) {[[aStation valueForKey:@"latitude"] doubleValue], [[aStation valueForKey:@"longitude"] doubleValue]}
stationPin.stationPosition = CLLocationCoordinate2DMake([[aStation valueForKey:@"latitude"] doubleValue], [[aStation valueForKey:@"longitude"] doubleValue]);