2011-10-06 49 views
1

因此,我有一个模型对象,它是一个名为'位置',其上有一组属性。其中之一是'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的值时,它不会被保留吗?

任何帮助,将不胜感激:)

+0

在'NSLog'调用中使用'%f',可能会有所帮助。 – Joost

+0

刚刚尝试过,而不是(n),现在我得到0.0000而不是:(谢谢反正! – JustinMoser

回答

0

所以事实证明,EXC_BAD_ACCESS错误是由joost和JBat100建议的。只需要使用%@而不是%f。但是我确实知道为什么我无法从控制器上的属性中获取用户位置值。这些方法被异步调用,并且forRowAtIndexPath方法在用户位置可以找到之前呈现表单元,因此指向userlocation的指针在被调用时没有指向任何东西。

我解决了这个问题,方法是添加一个活动指示器来查找用户位置,并将其保存到一个属性中,然后将该视图与表格一起推入屏幕,并将其属性设置为userlocation属性永不空:)

感谢大家的帮助!

0
location.currentLocation.coordinate.latitude 

是定义为

typedef double CLLocationDegrees; 

您只能使用%@用从NSObject的继承类实例类型CLLocationDegrees的(一般请讲)。你应该打印出双这样的:

NSLog(@"%f", location.currentLocation.coordinate.latitude); 

当您使用访问

self.userLocation = newLocation; 

如果财产被指定为保留值会被保留。

+0

我试过,不幸的是,当我这样做,而不是得到EXC_BAD_ACCESS错误,我只是得到0在日志中,和stil当你尝试并记录整个currentLocation对象时 – JustinMoser

+0

当你说“但是,当我从我的控制器类初始化'Location'模型的一个实例时,你的意思是你初始化一个新的CLLocation实例,或者你你自己定义了一个Location类吗?如果是这样的话,你可以将currentLocation作为一个静态类变量保存下来,但是记得在你得到一个位置时检查时间戳(系统可以给你一个你应该忽略的旧位置)。 – jbat100

+0

我自己定义了一个位置类,并具有一组属性:)上面的代码来自位置类。 – JustinMoser

0

发送到locationManager:didUpdateToLocation:fromLocation:(我希望你使用的是,而不是可靠的MKMapView委托方法)的值不是很可靠。从理论上讲,你应该逐步选择逐渐更准确的值。但是,通常会发送一个零值,正如您已经注意到并正在您的代码中检查的那样;但经常发送0.0,0.0的坐标,您应该检查并忽略该坐标。检查horizontalAccuracyverticalAccuracy属性的负值也是一个好主意,并且也会拒绝那些CLLocation

+0

当应用程序启动时,我的位置模型对象的实例被创建,并且位置模型上的其中一个属性是标签中需要的'distanceFromUserLocation'。 很显然,要计算出它需要知道你的当前位置,所以在模型对象中,我计算当前位置,并将newLocation值从didUpdateToLocation设置为我的currentLocation属性,以便distanceFromLocation可以使用它来执行它的计算。我得到的问题是newLocation有一个值,但只要将currentLocation设置为它的值,它就会失去值! – JustinMoser