使用未声明的标识符'locationManager'

时间:2014-11-12 22:15:42

标签: objective-c

我正在尝试按照本教程: http://www.appcoda.com/how-to-get-current-location-iphone-user/

一切都很好,直到我添加这一行: locationManager = [[CLLocationManager alloc] init]; 然后我得到了错误。

我也为这些行弄错:( Xcode建议我使用“_LongitudeLabel”?

if (currentLocation != nil) {
    longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
    latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}

知道什么是错的吗?教程是否有错误或我做错了什么? 谢谢!

这是ViewController.m文件:

#import "ViewController.h"

@implementation MyLocationViewController {
CLLocationManager *locationManager;
}
@end

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
locationManager = [[CLLocationManager alloc] init];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)getCurrentLocation:(id)sender {
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;

[locationManager startUpdatingLocation];
}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
                           initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;

if (currentLocation != nil) {
    longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
    latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}
}

@end

这是ViewController.h文件:

//  ViewController.h
//  MyLocationDemo
//
//  Created by Ian Nicoll on 12/11/14.
//  Copyright (c) 2014 Ian Nicoll. All rights reserved.
//
#import <CoreLocation/CoreLocation.h>
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *LatitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *LongitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
- (IBAction)getCurrentLocation:(id)sender;
@end

@interface MyLocationViewController : UIViewController <CLLocationManagerDelegate>
@end

2 个答案:

答案 0 :(得分:0)

第一个问题是您的错误。您在locationManager中声明了MyLocationViewController,但尝试在viewDidLoad ViewController中对其进行初始化,当然它不存在。

第二个问题是说明问题。声明@property时,默认行为是创建一个在其前面带下划线的实例变量。

因此@property (weak, nonatomic) IBOutlet UILabel *LatitudeLabel;可以作为self.latitudeLabel(通过setter / getter)或_latitudeLabel直接访问ivar。后者可能就是你想要的。

答案 1 :(得分:0)

好的,所以现在构建成功(虽然我不是100并且确定我把事情做对了)但现在我收到了这条线的警告:locationManager.delegate = self; - 从不兼容的类型&#39; ViewControler * const_strong&#39; 分配给&#39; id&#39; 你知道如何解决这个警告吗?

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController {
CLLocationManager *locationManager;
}

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
locationManager = [[CLLocationManager alloc] init];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)getCurrentLocation:(id)sender {
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;

[locationManager startUpdatingLocation];
}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
                           initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;

if (currentLocation != nil) {
    _LongitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
    _LatitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}
}

@end