设置CLLocationManager来解密错误代码,但我收到了一个未声明的标识符“错误”,我已经注释掉了。
为什么Xcode要求我声明'错误'作为标识符?我以为CLLocationManager会读入错误代码并将它们翻译成普通英语?
我在这里缺少什么?
CurrentLocationViewController.m
#import "CurrentLocationViewController.h"
@interface CurrentLocationViewController ()
@end
@implementation CurrentLocationViewController {
CLLocationManager *_locationManager;
CLLocation *_location;
BOOL _updatingLocation;
NSError *_lastLocationError;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
if((self = [super initWithCoder:aDecoder])) {
_locationManager = [[CLLocationManager alloc] init];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self updateLabels];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)getLocation:(id)sender
{
[self startLocationManager];
[self updateLabels];
}
#pragma mark - CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError %@", error);
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *newLocation = [locations lastObject];
if (error.code == kCLErrorLocationUnknown) { // Use of undeclared identifier 'error'
return;
}
[self stopLocationManager];
_lastLocationError = error; // Use of undeclared identifier 'error'
[self updateLabels];
}
-(void)updateLabels
{
if (_location != nil) {
self.latitudeLabel.text = [NSString stringWithFormat:@"%.8f", _location.coordinate.latitude];
self.longitudeLabel.text = [NSString stringWithFormat:@"%.8f", _location.coordinate.longitude];
self.tagButton.hidden = NO;
self.messageLabel.text = @"";
} else {
self.latitudeLabel.text = @"";
self.longitudeLabel.text = @"";
self.addressLabel.text = @"";
self.tagButton.hidden = YES;
NSString *statusMessage;
if (_lastLocationError == nil) {
if ([_lastLocationError.domain isEqualToString:kCLErrorDomain] && _lastLocationError.code == kCLErrorDenied) {
statusMessage = @"Location Services Disabled";
} else {
statusMessage = @"Error Getting Location";
}
} else if (![CLLocationManager locationServicesEnabled]) {
statusMessage = @"Location Services Disabled";
} else if (_updatingLocation) {
statusMessage = @"Searching...";
} else {
statusMessage = @"Press the Button to Start";
}
self.messageLabel.text = statusMessage;
}
}
-(void)startLocationManager
{
if ([CLLocationManager locationServicesEnabled]) {
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
[_locationManager startUpdatingLocation];
_updatingLocation = YES;
}
}
-(void)stopLocationManager
{
if (_updatingLocation) {
[_locationManager stopUpdatingLocation];
_locationManager.delegate = nil;
_updatingLocation = NO;
}
}
@end
答案 0 :(得分:1)
哦,jeez。你是谁,不是吗?
看起来你有2个方法处理一个名为“error”的变量。
第一个是locationManager:didFailWithError:
此方法是位置管理器在检测到错误状态时将调用的方法。
在该方法中,NSError对象作为“错误”传入。如果您想记录/保存您收到的错误,可以在此处进行操作。如果稍后需要,您应该将错误保存到_lastLocationError。在此方法中向用户显示错误也是一个好主意。
尝试引用名为“error”的变量的第二种方法是locationManager:didUpdateLocations:
该方法没有“错误”作为参数。变量名“错误”在这里没有意义。你不会在这里得到错误值,也不应该指望它们。
如果调用了您的locationManager:didFailWithError:方法,则可能永远不会调用您的locationManager:didUpdateLocations:方法,因为位置管理员已经告诉您存在问题并且无法处理您的请求。