我有一个应用程序,该应用程序创建一个包含(除其他事项外)一些位置数据的类实例。
在应用程序委托中,我设置了位置服务并开始获取位置数据;
//Delegate method to receive location information 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实例。
我的捕获类,当被调用时,其init方法被调用时将获取CLLocation;
//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信息。
我仍然不确定为什么原始解决方案不起作用,所以如果有人有任何见解,请多指教。
但是我已经解决了使用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"];
我需要打破lat,只要NSUserDefaults不存储CLLocation对象(NSCoding兼容性),就在captures类中重建它们;
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