xcode 将 self 分配给 CLLocationManager.delegate 会生成不兼容的类型警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5572397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:44:48  来源:igfitidea点击:

assigning self to CLLocationManager.delegate generates incompatible type warning

iphonexcodewarningscllocationmanager

提问by user278859

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...

我目前正在开发的应用程序已经有一段时间没有构建错误了,自从升级到 xCode 4 以来,一直给我此代码的最后一行不兼容的类型警告......

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.

有趣的是,经过清理和构建后,xCodes 没有报告任何问题。但是,如果我转到包含此代码的类,构建错误会突然出现并一直存在,直到我再次进行清理。

the actual warning is...

真正的警告是……

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

警告:语义问题:不兼容的指针类型从“类”分配给“id”

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?

CLLocationmanager 工作正常,我的委托方法被调用,所以一切似乎都正常工作。我想摆脱这个警告。我应该忽略它吗?

采纳答案by Erik B

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.

我认为您应该向 Apple 提交错误报告,直到他们对此采取措施之前,我想您所能做的就是忽略它。

回答by Glenn Howes

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.

您是否声明您的委托对象遵循 CLLocationManagerDelegate 协议?我通常会在类扩展中执行此操作,而不是在 MyObject 的头文件中执行此操作。

@interface MyObject()<CLLocationManagerDelegate> 
@end

or in Swift

或在斯威夫特

@objc class MyObject: NSObject, CLLocationManagerDelegate
{
// implement whichever of the optional protocol functions you need
}

回答by Tom Harrington

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 CLLocationManagerDelegateprotocol. 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.

不一致的警告似乎是一个错误。但真正的问题是self,上面代码中的类是否已经声明为实现了CLLocationManagerDelegate协议。如果你实现了正确的方法,那么无论它是如何声明的,它都会起作用。但是如果类声明不包含<CLLocationManagerDelegate>在其中,那么编译器就会抱怨。如果你没有,那么修复类声明应该修复警告。

回答by Sudheesh

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

我认为您必须在 .h 文件中添加 CLLocationManagerDelegate 作为

@interface MyViewController : UIViewController <CLLocationManagerDelegate>

回答by Nakyung Lim

I've got the same situation.

我有同样的情况。

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

添加下面的行,然后警告消息将消失。

#import <CoreLocation/CLLocationManagerDelegate.h>