由于“释放的对象”而无法传输值

时间:2012-09-07 08:22:36

标签: iphone objective-c

我正在尝试将lat \ lon传递给其他实例。 我添加了对lat \ lon of decive的调用并将其保存在NSString

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    lat = newLocation.coordinate.latitude;
    lon = newLocation.coordinate.longitude;
    latValueNSString = [NSString stringWithFormat: @"%f", lat];
    lanValueNSString = [NSString stringWithFormat: @"%f", lan];

}

lat,lon是浮动类型。

当我接受这个界面时

AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;

在debuuger模式中,我看到了lat,lon(float)的值,但是无法得到它们。当我访问latValueNSString/lanValueNSString时,我会看到它“freed object

我如何传递这些值?我的错在哪里? 用NSSunmber和同样的问题尝试了同样的事情

2 个答案:

答案 0 :(得分:0)

您正在分配autoreleased对象([NSString stringWithFormat: @"%f", lat]将返回一个utoreleased对象),这就是您收到该错误的原因。 您需要保留该值以供进一步使用。

只需更改您的方法:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    lat = newLocation.coordinate.latitude;
    lon = newLocation.coordinate.longitude;
    self.latValueNSString = [NSString stringWithFormat: @"%f", lat];
    self.lanValueNSString = [NSString stringWithFormat: @"%f", lan];

}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    lat = newLocation.coordinate.latitude;
    lon = newLocation.coordinate.longitude;
    latValueNSString = [[NSString stringWithFormat: @"%f", lat] retain];
    lanValueNSString = [[NSString stringWithFormat: @"%f", lan] retain];

}

答案 1 :(得分:-1)

对浮点变量使用assign属性,无需释放它。对于NSString变量,retain/release没问题。