0
下面的代碼用於使用MKLocalSearch
進行搜索並將結果加載到數組中。這個數組傳遞給我的[self.mapView addAnnotations:annotations]
方法。一切都很好,直到我嘗試通過點擊後退按鈕(在我的導航欄中查看故事板)來解除此viewcontroller
。使用iOS註釋和數組獲取錯誤訪問錯誤
我得到EXC_BAD_ACCESS(code=1, address=0x4)
。如果我將下面的顯示引腳部分註釋掉,問題就會消失(但我當然不會加載我的註釋)。
請幫忙!
-(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];
}
}];
}