我在 .m 文件中有以下代码。
该代码用于执行以下任务:
- 初始化 CLLocationManager 类
- 获取属性(速度、精度、坐标)
- 将这些值转换为 NSString
- 通过使用 NSLog 将这些值记录到屏幕上
- 设置指定标签中的值,然后最后
- 循环通过方法“locationUpdate”中包含的代码。
该代码为所有这些属性提供了一个初始值。但是,每个属性的值都是相同的(即 2.7188880)。
此外,当我通过方法“locationUpdate”循环时,代码为每个属性返回 0。
谁能告诉我代码没有返回属性值的原因?
- (void)viewDidLoad {
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 2.0
target: self
selector:@selector(locationUpdate)
userInfo: nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
NSString *spd = [[NSString alloc] initWithFormat:@"%f", speed];
NSString *crd = [[NSString alloc] initWithFormat:@"%f", coordinates];
NSString *drc = [[NSString alloc] initWithFormat:@"%f", direction];
NSString *acc = [[NSString alloc] initWithFormat:@"%f", accuracy];
NSString *desc = [locationManager description];
NSLog(@"Speed: %@", spd);
NSLog(@"Coordinates: %@", crd);
NSLog(@"Direction: %@", drc);
NSLog(@"Accuracy: %@", acc);
NSLog(@"Description: %@", desc);
Speedometer.text = spd;
CoordinatesLabel.text = crd;
DirectionLabel.text = drc;
AccuracyLabel.text = acc;
DescriptionLabel.text = desc;
}
- (void)locationUpdate {
NSString *desc = [locationManager description];
NSString *spd = [NSString stringWithFormat:@"%g", speed];
NSString *crd = [NSString stringWithFormat:@"%g", coordinates];
NSString *drc = [NSString stringWithFormat:@"%g", direction];
NSString *acc = [NSString stringWithFormat:@"%g", accuracy];
NSLog(@"Speed: %@", spd);
NSLog(@"Coordinates: %@", crd);
NSLog(@"Direction: %@", drc);
NSLog(@"Accuracy: %@", acc);
Speedometer.text = spd;
CoordinatesLabel.text = crd;
DirectionLabel.text = drc;
AccuracyLabel.text = acc;
DescriptionLabel.text = desc;
}