Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning to 'id<UINavigationControllerDelegate,UIImagePickerControllerDelegate>' from incompatible type 'ViewController *const__strong'

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];
like image 487
홍승욱 Avatar asked Oct 17 '14 13:10

홍승욱


2 Answers

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.

like image 57
memmons Avatar answered Oct 24 '22 11:10

memmons


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.

like image 23
Armand Avatar answered Oct 24 '22 10:10

Armand