使用iOS注释和数组获取错误访问错误

时间:2013-05-15 15:37:37

标签: ios arrays exc-bad-access

以下代码用于使用MKLocalSearch进行搜索并将结果加载到数组中。

此数组将传递给我的[self.mapView addAnnotations:annotations]方法。一切都很好,直到我尝试通过点击后退按钮(在我的故事板导航栏中)来解除此viewcontroller

我得到EXC_BAD_ACCESS(code=1, address=0x4)。如果我在下面注释掉Show Pins部分,问题就会消失(但我当然没有加载我的注释)。

请帮忙!

-(void)issueLocalSearchLookup:(NSString *)searchString usingPlacemarksArray:(NSArray *)placemarks {


self.coords =  mapView.userLocation.coordinate;


// Set the size of the region we want to get search results for.
MKCoordinateSpan span = MKCoordinateSpanMake(0.001250, 0.001250);
MKCoordinateRegion region = MKCoordinateRegionMake(mapView.userLocation.coordinate, span);   

[self.mapView setRegion:region animated:YES];



// Create the search request
self.localSearchRequest = [[MKLocalSearchRequest alloc] init];
self.localSearchRequest.region = region;
self.localSearchRequest.naturalLanguageQuery = searchString;

// Perform the search request...
self.localSearch = [[MKLocalSearch alloc] initWithRequest:self.localSearchRequest];
[self.localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {

    if(error){

        NSLog(@"localSearch startWithCompletionHandlerFailed!  Error: %@", error);
        return;

    } else {

        // We are here because we have data!

        for(MKMapItem *mapItem in response.mapItems){

            // Show pins...

            NSMutableArray *annotations = [NSMutableArray array];


            Annotation *annotation = [[Annotation alloc] initWithCoordinate: mapItem.placemark.location.coordinate];
            annotation.title = mapItem.name;
            annotation.subtitle = mapItem.placemark.addressDictionary[(NSString *)kABPersonAddressStreetKey];
            [mapView addAnnotation:annotation];
            NSLog(@"Name for result: = %@", mapItem.name);

              [self.mapView addAnnotations:annotations];

                NSLog(@"Name for result: = %@", mapItem.name);


        }

        MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01);
        MKCoordinateRegion region = MKCoordinateRegionMake(self.coords, span);
        [self.mapView setRegion:region animated:YES];


    }
}];


         }  

1 个答案:

答案 0 :(得分:0)

我已将自定义注释设置为NKPlacemark的子类....我需要将它作为NSObject的子类。