Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

assigning self to CLLocationManager.delegate generates incompatible type warning

The App I am current working on for some time now with no build errors has, since upgrading to xCode 4, been giving me an incompatible type warning for the last line of this code...

locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
locationManager.delegate = self;

The funny thing about it is that after a clean and build, xCodes reports no issues. However, if I go to the class containing this code the build error suddenly appears and remains until I do a clean again.

the actual warning is...

warning: Semantic Issue: Incompatible pointer types assigning to 'id' from 'Class'

CLLocationmanager works fine and my delegate methods are getting called so everything appears to be working properly. I would like to get rid of this warning. Should I just ignore it?

like image 204
user278859 Avatar asked Apr 06 '11 20:04

user278859


2 Answers

Did you declare your delegate object to follow the CLLocationManagerDelegate protocol? I'd usually do this in a class extension instead of the header file for MyObject.

@interface MyObject()<CLLocationManagerDelegate> 
@end

or in Swift

@objc class MyObject: NSObject, CLLocationManagerDelegate
{
// implement whichever of the optional protocol functions you need
}
like image 152
Glenn Howes Avatar answered Sep 28 '22 10:09

Glenn Howes


The inconsistent warnings seem like a bug. But the real question is whether self's class in the above code has been declared as implementing the CLLocationManagerDelegate protocol. If you implement the right methods then it'll work regardless of how it's declared. But if the class declaration doesn't include <CLLocationManagerDelegate> in it then the compiler will complain. If you don't have that then fixing the class declaration should fix the warning.

like image 26
Tom Harrington Avatar answered Sep 28 '22 09:09

Tom Harrington