I have an ImagePickerController in my application.
It works well, but beside ipc.delegate = self;
there appears an error message:
Assigning to 'id' from incompatible type 'ViewController *const__strong'
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];
If you take a look at the definition of the UIImagePickerController
delegate property, you'll see it defined as:
@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.
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With