홍승욱*_*홍승욱 7 xcode delegates incompatibletypeerror uiimagepickercontroller ios
我的应用程序中有一个ImagePickerController.它运行良好,但旁边ipc.delegate = self;出现一条错误消息:
从不兼容的类型'ViewController*const__strong'分配给'id'
该应用程序运行良好,所以我忽略了错误消息,但我想我需要知道原因.为什么会出现错误消息?
ipc = [[UIImagePickerController alloc]init];
ipc.modalPresentationStyle = UIModalPresentationCurrentContext;
ipc.delegate = self;
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[ipc setAllowsEditing:NO];
[self presentViewController:ipc animated:NO completion:nil];
Run Code Online (Sandbox Code Playgroud)
mem*_*ons 16
如果你看一下UIImagePickerControllerdelegate属性的定义,你会看到它被定义为:
@property(nonatomic, assign) id<UINavigationControllerDelegate,
UIImagePickerControllerDelegate> delegate
Run Code Online (Sandbox Code Playgroud)
无论您设置为委托的任何对象(在这种情况下,您正在使用self)都必须符合UINavigationControllerDelegate协议和UIImagePickerControllerDelegate协议.如果对象不符合这两个协议,您将收到编译时警告.
以下是您声明您的类符合协议的方式:
@interface MyViewController : UIViewController <UINavigationControllerDelegate,
UIImagePickerControllerDelegate>
Run Code Online (Sandbox Code Playgroud)
阅读使用协议,UINavigationControllerDelegate和UIImagePickerControllerDelegate.
我只是第一次遇到这个问题。如果您的类扩展了另一个符合其他协议的类,并且您的类也符合这两个协议,<UINavigationControllerDelegate, UIImagePickerControllerDelegate>那么为了删除警告,您必须像下面这样强制转换:
ipc.delegate = (id<<UINavigationControllerDelegate, UIImagePickerControllerDelegate>) self;
Run Code Online (Sandbox Code Playgroud)
就我个人而言,我认为这是编译器中的一个错误,因为您确实遵守两种协议,而您恰好也遵守其他协议。因此,您不必看到警告。
| 归档时间: |
|
| 查看次数: |
8197 次 |
| 最近记录: |