0

我正在尝试将 lat\lon 传递给其他实例。我添加了对 lat\lon 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;

在调试器模式下,我看到了 lat,lon (float) 的值,但无法找到它们。当我访问latValueNSString/lanValueNSString 我看到它“ freed object

我怎样才能传递这些值?我的错误在哪里?尝试了同样的事情NSSunmber和同样的问题

4

2 回答 2

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];

}
于 2012-10-04T17:58:57.680 回答
-1

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

于 2012-09-07T10:08:09.873 回答