CLLocation getting EXC_BAD_ACCESS
I am getting an Error when trying to access a CLLocation, can you please explain why?
CLLocation *currentLocation;
@property (nonatomic, retain) CLLocation *currentLocation;
I am geeting feedback for the location from the locatio开发者_Go百科n manager:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if (newLocation != nil) {
currentLocation = newLocation;
NSLog(@"locationManager: %@", currentLocation);
}
}
locationManager: <+36.45307493, +28.22220462> +/- 100.00m (speed -1.00 mps / course -1.00) @ 9/2/11 10:14:58 π.μ. GMT+02:00
but when I am trying to access currentLocation from DidSelectAnnotationView i get the EXC_BAD_ACCESS:
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
NSLog(@"current Location: %@", currentLocation);
}
can you please explain why I cannot access it?
Many Thanks!
You are not retaining the location. Do it like:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if (newLocation != nil) {
self.currentLocation = newLocation; /// The change is here
NSLog(@"locationManager: %@", currentLocation);
}
}
精彩评论