CLLocationDistance: Cant „getDistanceFrom“ an die Arbeit
-
19-09-2019 - |
Frage
Ansicht basierte Anwendung,
.m
- (void)viewDidLoad {
[super viewDidLoad];
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self;
[[self locationManager] startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
CurrentLocation = newLocation;
[locationManager stopUpdatingLocation];
myActivity.hidden=YES;
[self reStart];
}
-(void)reStart{
[self checkAndCreateDatabase]; //Sucess
[self readFromDatabase]; //Success if I comment the get distance part
[self sort]; //Success
}
-(void) readFromDatabase {
...
CLLocationCoordinate2D cord;
cord.latitude = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)] doubleValue];
cord.longitude =[[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)] doubleValue];
NSDate *today = [NSDate date];
CLLocation *objectLocation = [[CLLocation alloc] initWithCoordinate:cord altitude:1 horizontalAccuracy:1 verticalAccuracy:-1 timestamp:today];
CLLocationDistance distance = [objectLocation getDistanceFrom:CurrentLocation]/1000;
NSLog([NSString stringWithFormat:@"%.0f", distance]);
}
Was ist los?
Debugger sagt:
obj_msgSend
0x912e8688 <+0024> mov 0x20(%edx),%edi
Objekt aus Datenbank: lang: 59.291114 lat: 17.816191
Lösung
Is CurrentLocation
eine Instanz-Variable / Eigenschaft? Sie setzen es direkt ohne einen Accessor und Sie halten es nicht. Wenn es freigegeben wird, bevor Sie es in getDistance:
verwenden wird es zu einem Absturz führen.
Wenn es eine Eigenschaft ist, die self.CurrentLocation
zu verwenden, um sicherzustellen, dass sie erhalten bleibt.
Auch die Konstruktion ...
NSLog([NSString stringWithFormat:@"%.0f", distance]);
... ist riskant und überflüssig. Verwenden ...
NSLog(@"%.0f", distance);
... statt.
Andere Tipps
Mit distanceFromLocation: statt
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow