Приложение аварийно завершает работу при запросе свойства CLLocation из appDelegate - PullRequest
0 голосов
/ 28 декабря 2010

У меня есть приложение, которое создает экземпляр класса, который содержит (помимо прочего) некоторые данные о местоположении.

В приложении-делегате я настроил службы определения местоположения и начал собирать данные о местоположении;

//Delegate method to receive location infomation from locationManager
- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
           fromLocation:(CLLocation *)oldLocation
{


    latestLocation = newLocation;//Make latest location the same as NewLocation
    NSLog(@"Location is: %@", latestLocation);

}

Я объявляю последнее местоположение как свойство, чтобы я мог получить экземпляр CLLocation из другого класса.

Мой класс перехвата, когда вызывается, захватывает CLLocation при вызове его метода init;

//Designated initialiser
-(id) initWithVideoPath:(NSString *) vPath 
              userNotes:(NSString *) uNotes
         retentionState:(NSString *) rState

{

    //Call the super classes designated initializer
    [super init];

    //Get a pointer to the application delegate so we can access the location props
    Rolling_VideoAppDelegate *appDelegate = (Rolling_VideoAppDelegate*)[UIApplication sharedApplication].delegate;



    //If superclass failed to init
    if (!self)
        return nil;



    //Give the variables some initial values
    [self setVideoPath:vPath];
    [self setUserNotes:uNotes];
    [self setRetentionState:rState];
    dateCreated = [[NSDate alloc] init];


    mp = [[MapPoint alloc]initWithCoordinate:[[appDelegate latestLocation]coordinate]];//get the location from the coords from appDelegate

    return self;

    [dateCreated release];

}

Однако приложение вылетает при вызове инициализации mapPoint. Проблема в том, что я не получаю информацию о CLLocation надлежащим образом.

Может ли кто-нибудь помочь мне с этим.

Заранее спасибо,

Rich

1 Ответ

0 голосов
/ 28 декабря 2010

Я все еще не уверен, почему оригинальное решение не работает, поэтому, если у кого-то есть какие-то идеи, пожалуйста, просвещайте.

Я, однако, разработал немного не элегантную работу с использованием NSUserDefaults

    latestLocation = newLocation;//Make latest location the same as NewLocation

    //Use NSUser Defaults to save the CLLocation instance
    NSUserDefaults *location = [NSUserDefaults standardUserDefaults];
    [location setDouble:latestLocation.coordinate.latitude forKey:@"lat"];
    [location setDouble:latestLocation.coordinate.longitude forKey:@"longd"];

Мне нужно было разбить лат и до тех пор, пока NSUserDefaults не будет хранить объекты CLLocation (совместимость с NSCoding), реконструировать их в классе перехвата;

    NSUserDefaults *location = [NSUserDefaults standardUserDefaults];//Get a handle to the user defaults
    CLLocationDegrees lat = [location doubleForKey:@"lat"];
    CLLocationDegrees longd = [location doubleForKey:@"longd"];
    CLLocation *currentLocation = [[CLLocation alloc] initWithLatitude:lat longitude:longd];

    mp = [[MapPoint alloc]initWithCoordinate:[currentLocation coordinate]];//get the location from the coords from appDelegate
...