Warning in Custom Map Annotations iPhone

后端 未结 4 1250
南旧
南旧 2020-12-06 07:11

I am using a custom map annotation class for map view in iPhone. Whenever I pop my map view from navigation bar stack I usually see some warnings in console.

MapAnno

4条回答
  •  独厮守ぢ
    2020-12-06 07:50

    I was getting the same error as you:

    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:
     (
     Context: 0x0, Property: 0xa980ef0>
    

    As pointed here it was caused because I was adding a MKAnnotation with an invalid coordinate to a MKMapView.

    My solution was to create a function to check the coordinate valid.

    Place+MKAnnotation.m

    I created a category of my Place class and

    #import "Place.h"
    #import 
    
    @interface Place (MKAnnotation) 
    - (BOOL)coordinateIsValid;
    ...
    @end
    

    Place+MKAnnotation.m

    @implementation Place (MKAnnotation)
    
    - (BOOL)coordinateIsValid
    {
        return CLLocationCoordinate2DIsValid(CLLocationCoordinate2DMake([self.latitude doubleValue],[self.longitude doubleValue]));
    
    }
    ...
    @end
    

    I only add the annotation in my ViewController if the coordinate is valid.

    if([p coordinateIsValid]) {
        [self.mapView addAnnotation:p];
    } 
    

提交回复
热议问题