我正在尝试将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
和同样的问题尝试了同样的事情
答案 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
没问题。