尝试读取CLLocation newLocation设置的属性时的EXC_BAD_ACCESS

时间:2011-10-06 10:03:16

标签: objective-c exc-bad-access cllocationmanager cllocation

所以我有一个模型对象,它是一个名为'location',它有一组属性。其中一个是' distanceFromUserLocation '。显然,要知道它需要知道用户的位置,所以在模型对象中我正在计算用户的当前位置。

每次在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 的值。然后我记录lat和long以确保我实际上有一个值,我这样做。

然而,当我从我的控制器类初始化'Location'模型的实例,然后在该模型对象上记录currentLocation的值时:

NSLog(@"%@", location.currentLocation);

我在日志中得到(null)。

如果我尝试:

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

我收到 EXC_BAD_ACCESS 错误。

我成功获取didUpdateToLocation中的当前位置并对其进行loggint,但是当我将currentLocation属性设置为newLocation的值时,为什么它不会被保留?

对此的任何帮助将不胜感激:)

3 个答案:

答案 0 :(得分:0)

location.currentLocation.coordinate.latitude

类型为CLLocationDegrees,定义为

typedef double CLLocationDegrees;

您只能将%@与继承自NSObject的类实例一起使用(一般来说)。你应该打印出这样的双倍:

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

使用访问者时

self.userLocation = newLocation;

如果将属性指定为retain,则保留该值。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

事实证明,EXC_BAD_ACCESS错误归结为joost和JBat100建议的错误。只需使用%@而不是%f。但我确实弄明白为什么我无法从控制器上的属性中获取用户位置值。这些方法是异步调用的,forRowAtIndexPath方法在找到用户位置之前呈现表格单元格,因此指向userlocation的指针在调用时指向任何内容。

我通过添加一个带有活动指示器的视图来解决它,该活动指示器找到用户位置,将其保存到属性,然后将带有表的视图推入屏幕,设置它的userlocation属性,以便属性永远不会空:)

感谢大家的帮助!