簡體   English   中英

從不兼容的類型警告中分配

[英]Assigning to from incompatible type warning

從不兼容類型''CameraVIewController *''中分配id,顯示在下面的代碼中

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

實際上,我認為真正的原因是你錯過了協議“ UINavigationControllerDelegate

UIImagePickerController.h中,您可以看到委托定義:

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

所以,如果你想像這樣分配委托:

picker.delegate = self;

self viewController必須實現UINavigationControllerDelegateUIImagePickerControllerDelegate

如果你只實現了UIImagePickerControllerDelegate ,你找不到任何錯誤文字,但是你會收到一條警告“ 從不兼容的類型''分配給id''CameraVIewController ''*”,將你的viewController的協議聲明添加UINavigationControllerDelegate就可以了。

這是一個非常晚的答案,但我剛剛遇到並解決了它,希望它有所幫助。

你可能沒有聲明你的類符合UIImagePickerController。

@interface CameraVIewController : UIViewController <UIImagePickerControllerDelegate>

或者,您可以使用強制轉換修復警告:

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

但無論如何你必須實現UIImagePickerControllerDelegate

另一種方法是在實現文件之上聲明私有類或類擴展。 例如:

@interface CameraVIewController (Private) <UIImagePickerControllerDelegate>
@end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM