为自定义协议设置委托时收到警告 [英] getting a warning setting up delegate for a custom protocol

查看:26
本文介绍了为自定义协议设置委托时收到警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我向我的一个类添加了一个自定义协议,当我尝试在 prepareForSegue: 方法期间设置委托时,我收到了编译器警告.我得到的警告是...

i added a custom protocol to one of my classes and i am getting a compiler warning when i attempt to set the delegate during a prepareForSegue: method. the warning i get is...

Sending 'MyCustomViewControllerClass *const __strong' to parameter of incompatible type 'id<NSFileManagerDelegate>'

项目构建并运行,一切正常,没有警告.如果我将 添加到我的自定义类,警告就会消失.我错过了什么还是这是 Xcode (6 beta) 中的错误?该代码是用于设置协议/委托的标准代码,但无论如何我都会发布...

the project builds and runs and everything works fine minus the warning. if i add <NSFileManagerDelegate> to my custom class the warning goes away. am i missing something or is this a bug in Xcode (6 beta)? the code is standard code for setting up a protocol / delegate but i will post it anyways...

SomeSecondClass.h

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

SomeSecondClass.m

@interface SomeSecondViewController ()
…stuff

-(void)someMethod {

    [self.delegate doThisForMe];
}

@end

自定义类.h

#import <UIKit/UIKit.h>
#import " SomeSecondViewController.h"

@interface MyCustomViewController : UIViewController <SomeSecondCustomViewControllerDelegate>
//adding on <NSFileManagerDelegate> removes the warning...
@end

自定义类.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 的问题?

i have opened previous projects where the warning did not exist and now the same warning appears. i am wondering if this is an Xcode problem?

推荐答案

您遇到了一个问题,原因是 Objective-C 如何找到匹配的选择器和处理 id 引用的方式存在歧义.

You are running into an issue caused by an ambiguity in how Objective-C finds a matching selector and dealing with an id reference.

UIStoryboardSegue destinationViewController 返回一个 id.然后,您的代码尝试在此 id 引用上调用 setDelegate 方法.由于没有关于这个 id 实际引用什么的信息,它不知道你可能指的是哪个 setDelegate: 方法(有很多).所以编译器扫描它知道的列表并选择一个.在本例中,它选择了 NSFileManager 类中的 setDelegate: 方法.由于 self 不符合 NSFileManagerDelegate 协议,您会收到警告.

UIStoryboardSegue destinationViewController returns an id. Your code then tries to call the setDelegate method on this id reference. Since there is no information about what this id actually references, it doesn't know which setDelegate: method you might mean (there are many). So the compiler scans through the list it knows of and picks one. In this case it chose the setDelegate: method from the NSFileManager class. Since self doesn't conform to the NSFileManagerDelegate protocol, you get the warning.

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

You could ignore the warning and your code will work fine in this case.

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

The better solution is to help the compiler by adding a cast:

[(SomeSecondCustomViewController *)segue.destinationViewController setDelegate:self];

这会让编译器知道您真正指的是哪个 setDelegate: 方法.

This will let the compiler know which setDelegate: method you really mean.

顺便说一句 - 将 NSFileManagerDelegate 添加到您的类中并不是有效的解决方案,即使它目前有效.一些 import 语句的简单重新排序可能会导致编译器做出不同的选择,您的警告将返回但抱怨不符合其他一些协议.

BTW - Adding NSFileManagerDelegate to your class is not a valid solution even if it works at the moment. A simple reordering of some import statements could lead the compiler to make a different choice and your warning would return but complain about not conforming to some other protocol.

这篇关于为自定义协议设置委托时收到警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆