简体   繁体   English

从不兼容类型分配给Nullable

[英]Assigning to ID Nullable from Incompatible Types

I have this warning showing up in XCode that I can't seem to get rid of. 我在XCode中显示了我似乎无法摆脱的警告。

Assigning to 'id < UINavigationControllerDelegate, UIImagePickerControllerDelegate > _Nullable ' from incompatibale type 'MyViewController * const _strong' 从不兼容类型'MyViewController * const _strong'分配给'id <UINavigationControllerDelegate,UIImagePickerControllerDelegate> _Nullable'

on this line: 在这条线上:

picker.delegate = self;

This line of code causes the application to work as expected. 这行代码使应用程序按预期工作。 So removing it, it doesn't work. 因此删除它是行不通的。 But I don't know how to get rid of the error. 但是我不知道如何摆脱错误。 Any help? 有什么帮助吗?

Other methods that have delegate assigned are not throwing this warning. 指派了委托的其他方法不会引发此警告。

The view controller is a part of a TabBarViewController that is embedded in a NavigationController. 视图控制器是嵌入在NavigationController中的TabBarViewController的一部分。

My class inherits the UIImagePickerControllerDelegate 我的课继承了UIImagePickerControllerDelegate

@interface MyViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UIImagePickerControllerDelegate, UIActionSheetDelegate, UICollectionViewDelegateFlowLayout> {

}
///...
@end

And the complete method. 以及完整的方法。

- (void) showImagePickerForSourceType:(UIImagePickerControllerSourceType)sourceType {

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.modalPresentationStyle = UIModalPresentationCurrentContext;
    picker.sourceType = sourceType;
    picker.delegate = self; ///HERE IS THE ISSUE
    picker.modalPresentationStyle = UIModalPresentationFullScreen;
    picker.modalTransitionStyle   = UIModalTransitionStyleCoverVertical;

    if (sourceType == UIImagePickerControllerSourceTypeCamera) {
        picker.showsCameraControls = YES;
    }

    [self presentViewController:picker animated:YES completion:nil];
}

在此处输入图片说明

You need both the UINavigationControllerDelegate in addition to UIImagePickerControllerDelegate . 除了UIImagePickerControllerDelegate之外,还需要UINavigationControllerDelegate

@interface MyViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIActionSheetDelegate, UICollectionViewDelegateFlowLayout> {

}

Adding UINavigationControllerDelegate along with the UIImagePickerControllerDelegate hides that warning for me. UINavigationControllerDelegateUIImagePickerControllerDelegate一起添加对我隐藏了该警告。

In the UIKit the delegate for image picker is specified as: 在UIKit中,图像选择器的委托指定为:

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

From the Documentation: 从文档中:

The image picker's delegate object. 图像选择器的委托对象。

Declaration 宣言

SWIFT 迅速

weak var delegate: protocol<UIImagePickerControllerDelegate, UINavigationControllerDelegate>?

OBJECTIVE-C 目标C

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

Discussion 讨论区

The delegate receives notifications when the user picks an image or movie, or exits the picker interface. 当用户选择图像或电影或退出选择器界面时,委托会收到通知。 The delegate also decides when to dismiss the picker interface, so you must provide a delegate to use a picker. 委托还决定何时关闭选择器界面,因此您必须提供一个委托才能使用选择器。 If this property is nil, the picker is dismissed immediately if you try to show it. 如果此属性为nil,则尝试显示该选择器会立即将其关闭。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告:从“ NSArray”分配给“ NSMutableArray *”的指针类型不兼容 <NSString *> * _Nullable&#39; - Warning: Incompatible pointer types assigning to 'NSMutableArray *' from 'NSArray<NSString *> * _Nullable' 分配给&#39;id <UITextFieldDelegate> _Nullable&#39;来自不兼容类型&#39;HomeDataForm * const __strong&#39; - Assigning to 'id<UITextFieldDelegate> _Nullable' from incompatible type 'HomeDataForm *const __strong' 从不兼容的类型id_nullable分配给cllocationdegrees(&#39;aka double&#39;) - Assigning to cllocationdegrees ('aka double') from incompatible type id_nullable 分配给&#39;id <UIWebViewDelegate> _Nullable”来自不兼容类型“ WebView * const __strong” - Assigning to 'id<UIWebViewDelegate> _Nullable' from incompatible type 'WebView *const __strong' 不兼容的指针类型从NSString分配给“ UITextField” - Incompatible pointer types assigning to “UITextField” from NSString 从“ NSData *”分配给“ NSMutableData *”的指针类型不兼容 - Incompatible pointer types assigning to 'NSMutableData *' from 'NSData *' 不兼容的指针类型从“ UITableViewCell”分配给“ TableViewCell” - Incompatible pointer types assigning to 'TableViewCell' from 'UITableViewCell' 不兼容的指针类型从“ UIPopoverController *”分配给“ UIDatePicker *” - Incompatible pointer types assigning to 'UIDatePicker *' from 'UIPopoverController *' 从'nsarray'分配给'nsmutablearray'的不兼容指针类型 - incompatible pointer types assigning to 'nsmutablearray ' from 'nsarray ' 不兼容的指针类型从NSArray分配给NSMutableString - Incompatible pointer types assigning to NSMutableString from NSArray
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM