xcode 从不兼容的类型“ViewController *const__strong”分配给“id<UINavigationControllerDelegate,UIImagePickerControllerDelegate>”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26426710/
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
Assigning to 'id<UINavigationControllerDelegate,UIImagePickerControllerDelegate>' from incompatible type 'ViewController *const__strong'
提问by ???
I have an ImagePickerController in my application.
It works well, but beside ipc.delegate = self;
there appears an error message:
我的应用程序中有一个 ImagePickerController。它运行良好,但旁边ipc.delegate = self;
出现了一条错误消息:
Assigning to 'id' from incompatible type 'ViewController *const__strong'
从不兼容的类型“ViewController *const__strong”分配给“id”
The app workes well, so I ignored the error message, but I think I need to know why. Why is the error message appearing?
该应用程序运行良好,因此我忽略了错误消息,但我想我需要知道原因。为什么会出现错误信息?
ipc = [[UIImagePickerController alloc]init];
ipc.modalPresentationStyle = UIModalPresentationCurrentContext;
ipc.delegate = self;
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[ipc setAllowsEditing:NO];
[self presentViewController:ipc animated:NO completion:nil];
回答by memmons
If you take a look at the definition of the UIImagePickerController
delegate property, you'll see it defined as:
如果您查看UIImagePickerController
委托属性的定义,您会看到它被定义为:
@property(nonatomic, assign) id<UINavigationControllerDelegate,
UIImagePickerControllerDelegate> delegate
Whatever object you set as the delegate (in this case you are using self
) must conform to both the UINavigationControllerDelegate
protocol and the UIImagePickerControllerDelegate
protocol. If the object does not conform to both of these protocols, you'll get a compile-time warning.
无论您设置为委托的任何对象(在本例中您正在使用self
)都必须符合UINavigationControllerDelegate
协议和UIImagePickerControllerDelegate
协议。如果对象不符合这两种协议,您将收到编译时警告。
Here's how you declare that your class conforms to the protocols:
以下是您如何声明您的类符合协议:
@interface MyViewController : UIViewController <UINavigationControllerDelegate,
UIImagePickerControllerDelegate>
Read up on working with protocols, UINavigationControllerDelegate, and UIImagePickerControllerDelegate.
阅读使用协议、UINavigationControllerDelegate和UIImagePickerControllerDelegate。
回答by Armand
I just ran into this for the first time. If your class extends another class which conforms to other protocols, and your class also conforms to these two protocols <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
then in order to remove the warning, you must cast it, like this:
我只是第一次遇到这个。如果您的类扩展了另一个符合其他协议的类,并且您的类也符合这两个协议,<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
那么为了删除警告,您必须强制转换它,如下所示:
ipc.delegate = (id<<UINavigationControllerDelegate, UIImagePickerControllerDelegate>) self;
Personally, I think this is a bug in the compiler, in that you do adhere to both protocols, you just also happen to adhere to others. Therefore, you shouldn't have to see the warning.
就个人而言,我认为这是编译器中的一个错误,因为您确实遵守了这两种协议,但您也恰好遵守了其他协议。因此,您不应该看到警告。