2010-10-18 29 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