2011-05-27 85 views
1

当我有一个CLLocation我下面的函数应用崩溃访问CLLocation

- (void)locationManager:(CLLocationManager *)manager 
didUpdateToLocation:(CLLocation *)newLocation 
     fromLocation:(CLLocation *)oldLocation{ 

currentLocation = newLocation; 
} 

内设置为当前位置。当我试图访问在以后的方法中的当前位置的应用程序崩溃。

NSLog(@"current latitude is %f", currentLocation.coordinate.latitude); 
NSString *longitude = [NSString stringWithFormat:@"%g", currentLocation.coordinate.longitude]; 
NSString *latitude = [NSString stringWithFormat:@"%g", currentLocation.coordinate.latitude]; 

当前位置在第一个方法中设置。我可以打印出来,奇怪的是,我将这个currentLocation定义为标题中的指针。

该应用程序崩溃,没有堆栈跟踪。调试器只是发送给我的应用程序的retValue。

我试图在设置到newLocation之前保留指针和alloc,但似乎没有任何工作。

我缺少的是:d

+0

如果您使用%f而不是%g,该怎么办?或者尝试使currentLocation属性,所以你会有'self.currentLocation'而不是? – 2011-05-27 01:48:26

+0

如何在你的.h文件中声明'currentLocation'? – 2011-05-27 01:54:56

+0

CLLocation * currentLocation; @property(nonatomic,retain)CLLocation * currentLocation; – jarryd 2011-05-27 01:57:12

回答

4

我不太确定你的代码,但我可以建议该

我试图将其设置为到newLocation之前保留指针以及页头,但似乎没有任何工作。

您需要在设置之前保留newLocation指针而不是currentLocation。

currentLocation = [newLocation retain]; 

self.currentLocation = newLocation; 

因为你的任务currentLocation = newLocation;不保留指针和newLocation是自动释放。

+0

这表示感谢 – jarryd 2011-05-27 12:27:25

1

应用崩溃并没有 堆栈跟踪。调试器只是发送我 到我的应用程序的retValue。

您使用的是Xcode 4吗?很多人在新界面上遇到麻烦。在堆栈跟踪窗口的底部有一个滑块,如果它设置为“粗略”,它将只显示应用程序的main()块。

正如以上评论所示,我认为你应该说self.currentLocation而不是currentLocation。不同的是,当你只说currentLocation时,你只是分配一个指针。要获得您在头文件中定义的所有“魔术”功能(保留,等),您需要使用self.currentLocation表示法或OR [self setCurrentLocation:newLocation];。功能上这些都是一样的,这是一个风格问题。