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

홍승욱 picture 홍승욱 · Oct 17, 2014 · Viewed 9.8k times · Source

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];

Answer

memmons picture memmons · Oct 17, 2014

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.