简体   繁体   中英

Assigning to from incompatible type warning

Assigning to id from incompatible type ''CameraVIewController*'' warning showing at the below code

 UIImagePickerController * picker = [[UIImagePickerController alloc] init];
 picker.delegate = self;

Actually, I think the real reason is that you miss the protocol " UINavigationControllerDelegate "

in UIImagePickerController.h you can see the delegate define:

@property(nonatomic,assign)    id <UINavigationControllerDelegate, UIImagePickerControllerDelegate> delegate;

So if you want to assign delegate like this:

picker.delegate = self;

the self viewController must implement both UINavigationControllerDelegate and UIImagePickerControllerDelegate

If you only implement UIImagePickerControllerDelegate , you can not find any error literal, but you will get a warning " Assigning to id from incompatible type ''CameraVIewController ''*", add UINavigationControllerDelegate to your viewController's protocol declaration shall get rid of it.

It's a very late answer, but I just met and solved it, hope it helps.

你可能没有声明你的类符合UIImagePickerController。

@interface CameraVIewController : UIViewController <UIImagePickerControllerDelegate>

Alternatively, you can fix the warning with a cast:

picker.delegate = (id <UIImagePickerControllerDelegate>) self;

But you have to implement UIImagePickerControllerDelegate anyway.

Another way is to declare private category or class extension on top of your implementation file. For example:

@interface CameraVIewController (Private) <UIImagePickerControllerDelegate>
@end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM