我和你一样遇到了同样的错误:
An instance 0xa975400 of class xxxxxx was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info:
<NSKeyValueObservationInfo 0xa980eb0> (
<NSKeyValueObservance 0xa980e70: Observer: 0xa94f8a0, Key path: coordinate, Options: <New: NO, Old: NO, Prior: YES> Context: 0x0, Property: 0xa980ef0>
如此指出,这是因为我正在向MKMapView添加一个带有无效坐标的MKAnnotation。
我的解决方案是创建一个函数来检查坐标是否有效。
Place+MKAnnotation.m
我创建了一个我的地点类的分类,并且
#import "Place.h"
#import <MapKit/MapKit.h>
@interface Place (MKAnnotation) <MKAnnotation>
- (BOOL)coordinateIsValid;
...
@end
Place+MKAnnotation.m
@implementation Place (MKAnnotation)
- (BOOL)coordinateIsValid
{
return CLLocationCoordinate2DIsValid(CLLocationCoordinate2DMake([self.latitude doubleValue],[self.longitude doubleValue]));
}
...
@end
只有在坐标有效时,我才会在我的ViewController中添加注释。
if([p coordinateIsValid]) {
[self.mapView addAnnotation:p];
}