ios – 获取警告设置自定义协议的委托

ios – 获取警告设置自定义协议的委托,第1张

概述我在我的一个类中添加了一个自定义协议,当我尝试在prepareForSegue:方法期间设置委托时,我收到编译器警告.我得到的警告是…… Sending 'MyCustomViewControllerClass *const __strong' to parameter of incompatible type 'id<NSFileManagerDelegate>' 项目构建和运行,一切正常,减去 我在我的一个类中添加了一个自定义协议,当我尝试在prepareForSegue:方法期间设置委托时,我收到编译器警告.我得到的警告是……

Sending 'MyCustomVIEwControllerClass *const __strong' to parameter of incompatible type 'ID<NSfileManagerDelegate>'

项目构建和运行,一切正常,减去警告.如果我添加< NSfileManagerDelegate>我的自定义类警告消失了.我错过了什么,或者这是Xcode(6 beta)中的错误?代码是设置协议/委托的标准代码,但无论如何我都会发布它…

SomeSecondClass.h

#import <UIKit/UIKit>@class SomeSecondCustomVIEwController;@protocol SomeSecondCustomVIEwControllerDelegate <NSObject>- (voID)doThisForMe@end@interface SomeSecondCustomVIEwController : UIVIEwController@property (weak,nonatomic) ID <SomeSecondCustomVIEwControllerDelegate> delegate;@end

SomeSecondClass.m

@interface SomeSecondVIEwController ()…stuff-(voID)someMethod {    [self.delegate doThisForMe];}@end

CustomClass.h

#import <UIKit/UIKit.h>#import “ SomeSecondVIEwController.h”@interface MyCustomVIEwController : UIVIEwController <SomeSecondCustomVIEwControllerDelegate>//adding on <NSfileManagerDelegate> removes the warning...@end

CustomClass.h

...standard stuff...- (voID)prepareForSegue:(UIStoryboardSegue *)segue sender:(ID)sender{    if ([[segue IDentifIEr] isEqualToString:@"MySegue"]) {         //this is where the warning happens on "self"         [segue.destinationVIEwController setDelegate:self];    }}- (voID)doThisForMe {   //doing some stuff...}@end

我已经打开了之前没有警告的项目,现在出现了相同的警告.我想知道这是否是一个Xcode问题?

解决方法 您遇到的问题是Objective-C如何找到匹配的选择器并处理ID引用时出现歧义.

UIStoryboardSegue destinationVIEwController返回一个ID.然后,您的代码尝试在此ID引用上调用setDelegate方法.由于没有关于此ID实际引用的信息,因此它不知道您可能指的是哪个setDelegate:方法(有很多).因此,编译器扫描它知道的列表并选择一个.在这种情况下,它从NSfileManager类中选择了setDelegate:方法.由于self不符合NSfileManagerDelegate协议,因此会收到警告.

您可以忽略该警告,在这种情况下您的代码将正常工作.

更好的解决方案是通过添加强制转换来帮助编译器:

[(SomeSecondCustomVIEwController *)segue.destinationVIEwController setDelegate:self];

这将让编译器知道你真正想要的setDelegate:方法.

顺便说一句 – 将NSfileManagerDelegate添加到您的类中并不是一个有效的解决方案,即使它现在正常工作.对某些import语句进行简单的重新排序可能会导致编译器做出不同的选择,并且您的警告会返回,但会抱怨不符合其他协议.

总结

以上是内存溢出为你收集整理的ios – 获取警告设置自定义协议的委托全部内容,希望文章能够帮你解决ios – 获取警告设置自定义协议的委托所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1002612.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存