我的核心位置有效,但我在这行代码中收到警告。 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.
}
答案 0 :(得分:6)
将您的类声明为实现位置管理器委托的协议。
@interface phoneLocationViewController : UIViewController <CLLocationManagerDelegate> {
答案 1 :(得分:3)
您应该将CLLocationManagerDelegate
添加到您的界面声明中。
@interface phoneLocationViewController : UIViewController <CLLocationManagerDelegate> {
....
}