开发者

assigning self to CLLocationManager.delegate generates incompatible type warning

开发者 https://www.devze.com 2023-02-22 08:34 出处:网络
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...

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: Incompat开发者_StackOverflow中文版ible 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?


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
}


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.


I think you should file a bug report with Apple and until they do something about it I guess all you can do is ignore it.


I think you have to add the CLLocationManagerDelegate on your .h file as

@interface MyViewController : UIViewController <CLLocationManagerDelegate>


I've got the same situation.

add the line below then the warning message will be gone.

#import <CoreLocation/CLLocationManagerDelegate.h>
0

精彩评论

暂无评论...
验证码 换一张
取 消