MKMapKit - 循环访问MKAnnotations时的EXC_BAD_ACCESS

时间:2011-06-03 00:39:39

标签: objective-c ios ios4 mkmapview mkannotation

我已经在2天内遇到了这个EXC_BAD_ACCESS错误。我有一个reloadAnnotations方法,在添加新注释之前删除所有注释。在删除注释之前,此方法应检查新集合是否包含相同的位置,以便不删除并重新添加。但是一旦我试图找出当前的注释标题,我就会收到此错误Thread 1: Program received signal: "EXC_BAD_ACCESS"

当我在调试器中查看注释时,title属性显示“无效摘要”。它必须是由一个没有被保留的值引起的,但我已经尝试了所有的东西而且无法弄明白。

为什么我不能将注释标题记录到NSLog?

为什么我不能将每个标题和坐标与其他对象进行比较?

BrowseController.m

-(void)reloadAnnotations
{
    NSMutableArray *toRemove = [NSMutableArray arrayWithCapacity:10];
    for (id annotation in _mapView.annotations) {
        if (annotation != _mapView.userLocation) {
            //ParkAnnotation *pa = (ParkAnnotation *)annotation;
            ParkAnnotation *pa = annotation;
            NSLog(@"pa.title %@", pa.title); // Thread 1: Program received signal: "EXC_BAD_ACCESS"
            [toRemove addObject:annotation];
        }
    }
    // DON'T REMOVE IT IF IT'S ALREADY ON THE MAP!!!!!! 
    for(RKLocation *loc in locations) 
    {
        CLLocationCoordinate2D location;
        location.latitude = (double)[loc.lat doubleValue];
        location.longitude = (double)[loc.lng doubleValue];
        ParkAnnotation *parkAnnotation = [[ParkAnnotation alloc] initWithTitle:loc.name andCoordinate:location];      
        [_mapView addAnnotation:parkAnnotation];
    }
    [_mapView removeAnnotations:toRemove];
}



- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
    NSLog(@"BrowseViewController map viewForAnnotation");
    MKPinAnnotationView *pin = (MKPinAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier: @"anIdentifier"];

    if (pin == nil){
        pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation
                                               reuseIdentifier: @"anIdentifier"] autorelease];

        pin.pinColor = MKPinAnnotationColorRed;
        pin.animatesDrop = YES;
        pin.canShowCallout = YES;
    }
    else{
        pin.annotation = annotation;
    }
    return pin;
    }

ParkAnnotation.h

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

@interface ParkAnnotation : NSObject <MKAnnotation> {

    NSString *title;
    CLLocationCoordinate2D coordinate;

}


@property (nonatomic, copy) NSString *title;  
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d;

@end

ParkAnnotation.m(编辑:见沃尔夫冈的评论)

#import "ParkAnnotation.h"
@implementation ParkAnnotation
@synthesize title, coordinate;
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d {
    self = [super init];
    if (self) { 
        title = ttl;
        coordinate = c2d;    
    } 
    return self;
}
- (void)dealloc {
    [title release];
    [super dealloc];
}
@end

2 个答案:

答案 0 :(得分:3)

虽然您已声明title具有copy类型属性,但它永远不会被复制,因为您不使用setter方法并直接分配。你甚至没有所有权就发布它。像这样改变,

title = [ttl copy];

答案 1 :(得分:0)

ParkAnnotation.m 中的初始化程序不是遵循ObjC约定编写的。永远不会设置 self 变量,类的指定初始值设定项应遵循以下模式:

- (id)init 
{
    self = [super init]; 
    if (self) 
    { 
        /* custom initialization here ... */ 
    } 
    return self;
} 

由于未设置 self ,调用者中使用的访问者方法将失败;当尝试从另一个类访问对象内的属性时,容器对象(在 self 中引用的 ParkAnnotation.m )将为nil或某些虚假值。