0
如果我的问题是如此愚蠢的问题,我很抱歉提前。 我试图编写一个缺乏编程基础知识的应用程序。 我应用此功能从某人在stackoverflow论坛。然后我编辑了一下。如何在使用后清洁变量或功能
-(void)computeDistanceFrom:(CLLocation *)oldL tO:(CLLocation *)newL {
NSLog(@"oldL %@",oldL);
NSLog(@"newL %@",newL);
CLLocationDistance currentDistance = [oldL distanceFromLocation:newL];
NSLog(@"you have travel=%f",currentDistance);
distance = distance + currentDistance;
distanceInKm = distance/1000;
carbonSaveKg = distanceInKm*0.069565217;
fuelSaveLit = distanceInKm*0.073913043;
//set string to show
distanceLabelValue = [[NSString stringWithFormat:@"%1.3f",distanceInKm]retain];
distanceLabel.text = distanceLabelValue;
NSLog(@"distanceFromView=%f",distanceInKm);
carbonDioxideLabelValue = [[NSString stringWithFormat:@"%1.3f",carbonSaveKg]retain];
carbonDioxideLabel.text = carbonDioxideLabelValue;
NSLog(@"CO2FromView=%f",carbonSaveKg);
fuelLabelValue = [[NSString stringWithFormat:@"%1.2f",fuelSaveLit]retain];
fuelLabel.text = fuelLabelValue;
NSLog(@"FuelFromView=%f",fuelSaveLit);
}
我的应用程序跟踪用户的当前位置。然后它通过标签显示距离和一些值。当我离开它包含此功能的看法,我想给变量distanceInKm
,carbonSaveKG
和fuelLit
的值设置为0。
我的问题是,当我回到这一观点再次,该变量的值仍然像以前一样。 请帮帮我。
非常感谢您的帮助。
感谢您的回答@Matic。我试过这个函数,它看起来像刚刚清除标签的函数,但没有清除保持该值的变量。谢谢。 – 2012-03-06 16:34:34
我不明白你真正想要做什么..你也可以为所有值调用distanceLabelValue = @“”和distanceInKm = 0。但也知道这个代码可能会有内存泄漏,因为你的字符串被保留了下来,所以一定要在给它们分配另一个值之前调用它们。 – 2012-03-07 07:29:46
我对这些东西很好奇。起初,我没有保留它们,编译器返回内存泄漏问题'BAD_EXC_ACCESS'。我保留他们之后,错误消失了。 :(更多的,我已经尝试distanceInKm = 0;但结果仍然是一样的,我猜想,也许是因为保留,所以变量仍然保持包含值 – 2012-03-08 00:40:25