2010-10-18 67 views
0

我有CLLocation變量聲明,我給它的值,但是當我在其他地方使用它,與應用程序崩潰控制檯「調試終止」沒有任何日誌應用程序崩潰與「調試終止」

CLLocation *userLoc; 

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    userLoc=newLocation; 
    [self somefunction]; 
} 
-(void) somefunction 
{ 
NSLog(@"%@",userLoc); 
} 

這裏登錄userLoc正確

但在我的另一種方法

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*) indexPath 
{ 
NSLog(@"%@",userLoc); 
} 

這裏應用程序崩潰。 請幫忙

回答

4

核心位置爲delegate方法提供了自動釋放的CLLocation對象,所以它在該方法之外變得無效。爲了保護它,你需要保留位置值:

userLoc=[newLocation retain]; 

或者,更好,聲明屬性與保留的屬性,並使用它通過以下方式您userLoc變量:

self.userLoc = newLocation; 

附: Memory management guide真的是一個必讀...

+0

我沒有聲望投票你回答了......但+1爲你的答案..明白瞭解 – 2010-10-18 10:14:54