2011-05-27 44 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];。功能上這些都是一樣的,這是一個風格問題。

相關問題