Alex Rivera | Logout

MKMapView - Removing annotation causes app crash

Asked 2012-06-13T09:35:15.460
11

Removing annotations from my map view in the following way:

 if ([[self.mapView annotations] count] > 0)
{
    [self.mapView removeAnnotations:[self.mapView annotations]];
}

causes my application to crash with the following exception:

*** 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.'

The annotations are added in the following way:

 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];        


}

My PFAnnotation.h is:

@interface PFAnnotation : NSObject <MKAnnotation>
{
    NSString *stationName;
    CLLocationCoordinate2D stationPosition;
    NSNumber *stationLength;

}

@property (nonatomic, retain) NSString *stationName;
@property CLLocationCoordinate2D stationPosition;
@property (nonatomic, retain) NSNumber *stationLength;


@end

and my PFAnnotation.m is:

@implementation PFAnnotation

@synthesize stationName;
@synthesize stationPosition;
@synthesize stationLength;


- (CLLocationCoordinate2D)coordinate;
{
    return stationPosition; 
}

- (NSString *)title
{
    return stationName;

}

- (NSString *)subtitle
{
    if (stationLength
Edit
Report

1 Answer

1

If Your PFAnnotation really has incorrect setters getters for string values:

from here: http://cocoadevcentral.com/d/learn_objectivec/

Setter:

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

Getter:

- (NSString*) caption 
{
    return caption;
}

Release:

- (void) dealloc
{
    [caption release];
    [super dealloc];
}

also - it's easier to provide coordinates in this way: (also works on ios 3.1.3)

stationPin.stationPosition = (CLLocationCoordinate2D) {[[aStation valueForKey:@"latitude"] doubleValue], [[aStation valueForKey:@"longitude"] doubleValue]}

than (only from ios 4)

stationPin.stationPosition = CLLocationCoordinate2DMake([[aStation valueForKey:@"latitude"] doubleValue], [[aStation valueForKey:@"longitude"] doubleValue]);
answered 2012-06-13T10:21:25.097

Your Answer