1

我的核心位置有效,但我在这行代码中收到警告。locationManager.delegate = self; 警告是从不兼容的类型“phoneLocationViewController *const __strong”分配给“id”。我如何摆脱这个警告?这是我的代码

。H

@interface phoneLocationViewController : UIViewController {

}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation    
*)newLocation fromLocation:(CLLocation *)oldLocation;

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error;



@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, retain) CLLocation *currentLocation;

.m

@synthesize locationManager, currentLocation;

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation  
*)newLocation fromLocation:(CLLocation *)oldLocation {
self.currentLocation = newLocation;

if(newLocation.horizontalAccuracy <= 100.0f) { [locationManager stopUpdatingLocation]; }
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
if(error.code == kCLErrorDenied) {
    [locationManager stopUpdatingLocation];
} else if(error.code == kCLErrorLocationUnknown) {
    // retry
} else {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error retrieving location"
                                                    message:[error description]
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
}
}

- (void)viewDidLoad
{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self; (I GET THE WARNING HERE)
[locationManager startUpdatingLocation];

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
[locationManager stopUpdatingLocation];

[super viewDidUnload];
// Release any retained subviews of the main view.
}
4

2 回答 2

6

将您的类声明为实现位置管理器委托的协议。

@interface phoneLocationViewController : UIViewController <CLLocationManagerDelegate> {
于 2012-09-07T13:44:51.033 回答
3

您应该添加CLLocationManagerDelegate到您的接口声明。

@interface phoneLocationViewController : UIViewController <CLLocationManagerDelegate> {
    ....
}
于 2012-09-07T13:45:05.453 回答