因此,我有一个模型对象,它是一个名为'位置',其上有一组属性。其中之一是'distanceFromUserLocation'。显然,要知道它需要知道用户的位置,所以在模型对象中,我正在计算用户的当前位置。EXC_BAD_ACCESS当试图读取由CLLocation设置的属性newLocation
这也会在didUpdateToLocation方法上每次都在此模型上设置一个CLLocation属性。
我也有一个initWithDictionary方法在模型上设置属性的位置模型的所有值。下面
代码为我的模型:
@synthesize name = _name;
@synthesize entryLocation = _entryLocation;
@synthesize address = _address;
@synthesize contactNumber = _contactNumber;
@synthesize distanceFromUserLocation = _distanceFromUserLocation;
@synthesize itemDescription = _itemDescription;
@synthesize locationManager = _locationManager;
@synthesize userLocation = _userLocation;
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if (newLocation != nil) {
self.userLocation = newLocation;
NSLog(@"lat: %@ lon: %@ ", [NSString stringWithFormat:@"%.7f", newLocation.coordinate.latitude], [NSString stringWithFormat:@"%.7f", newLocation.coordinate.longitude]);
}
[manager stopUpdatingLocation];
}
-(id)initWithDictionary:(NSDictionary*) dictionary
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
self = [super init];
if (self)
{
self.name = [dictionary valueForKey:@"Name"];
self.address = [dictionary valueForKey:@"Address"];
self.contactNumber = [dictionary valueForKey:@"ContactNumber"];
self.itemDescription = [dictionary valueForKey:@"Description"];
double lat = [[dictionary valueForKey:@"Latitude"] doubleValue];
double lon = [[dictionary valueForKey:@"Longitude"] doubleValue];
self.entryLocation = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
self.distanceFromUserLocation = [self.userLocation distanceFromLocation:self.entryLocation];
}
return self;
}
我的问题是,在didUpdateToLocation,我设置 'self.currentLocation' 属性的newLocation值。然后我记录经纬度,确保我确实有一个价值,我确实有这个价值。
然而,当我再从我的控制器类初始化“位置”模型的实例,然后登录该模型对象上currentLocation值:
NSLog(@"%@", location.currentLocation);
我得到(空)在日志中。
如果我尝试:
NSLog(@%@", location.currentLocation.coordinate.latitude);
我得到一个EXC_BAD_ACCESS 错误。
我成功获取didUpdateToLocation中的当前位置并记录它,但为什么当我将currentLocation属性设置为newLocation的值时,它不会被保留吗?
任何帮助,将不胜感激:)
在'NSLog'调用中使用'%f',可能会有所帮助。 – Joost
刚刚尝试过,而不是(n),现在我得到0.0000而不是:(谢谢反正! – JustinMoser