【问题标题】:Cannot find protocol definition for XXX找不到 XXX 的协议定义
【发布时间】:2018-12-20 16:54:40
【问题描述】:

我先声明了一个协议,然后使用它。但我收到警告“找不到 LeveyPopListViewDelegate 的协议定义”。 代码如下:

@protocol LeveyPopListViewDelegate;

@interface LeveyPopListView : UIView <LeveyPopListViewDelegate,UITableViewDataSource,   UITableViewDelegate,UITextFieldDelegate>

//the content of LeveyPopListView

@end

@protocol LeveyPopListViewDelegate <NSObject>
//the definition for LeveyPopListViewDelegate
@end

如果我首先定义LeveyPopListViewDelegate,我不能在协议中使用LeveyPopListView。

【问题讨论】:

  • Max_:虽然我们过去用委托做的一些事情现在用块做的更好,但有很多理由使用协议。
  • 您使用的是哪个版本的 XCode?​​span>
  • Xcode 版本 4.6 (4H127)
  • 没关系...您的代码运行良好,对我来说没有错误(XCode4.6)-我只是在一些旧版本中尝试过,以防发生了变化,但在 4.2 中也可以;-j
  • 但奇怪的是,尽管有警告,但它确实有效,对吧?

标签: objective-c protocols


【解决方案1】:

我最终取消了该特定行的所有警告,这并不理想但效果很好。

// Forward-declare protocols (to avoid circular inclusion)
@protocol YourProtocol;
#pragma clang diagnostic push
// To get rid of 'No protocol definition found' warnings which are not accurate
#pragma clang diagnostic ignored "-Weverything" 

@interface YourClass: NSObject
// <YourProtocol>
#pragma clang diagnostic pop

确保您执行 push & pop,否则您最终会忽略此文件的所有警告!

【讨论】:

【解决方案2】:

如果您遵循了这篇文章中的好建议,但问题仍然存在,请尝试进行清理。我在它上面挠了一阵子,然后清理解决了这个问题。

【讨论】:

    【解决方案3】:

    我总是这样:

    @class LeveyPopListView;
    
    @protocol LeveyPopListViewDelegate <NSObject>
    //the definition for LeveyPopListViewDelegate
    @end
    
    @interface LeveyPopListView : UIView <LeveyPopListViewDelegate,UITableViewDataSource,   UITableViewDelegate,UITextFieldDelegate>
    
    //the content of LeveyPopListView
    
    @end
    

    【讨论】:

    • 但是我想在protocolLeveyPopListViewDelegate中使用LeveyPopListView
    • 您可以使用我发布的内容。这就是前向类声明的重点。
    • 非常感谢。但是为什么先声明协议会收到警告呢?
    • 我一直无法使前向协议定义工作,所以我总是按照我发布的那样做。
    【解决方案4】:

    另外,如果你在不同的文件中声明和定义协议,你必须#import那个文件,前向声明在这种情况下没有帮助。

    顺便说一句。切勿像某些答案所建议的那样隐藏警告。

    【讨论】:

      【解决方案5】:

      在协议定义中必须使用具体的类名是不寻常的。您应该做的(大多数情况下,可能会出现异常……尽管它们很臭)是使您的类符合协议,并在委托协议定义中使用该类。

      一个类需要成为它自己的委托也是一种不好的气味。我会仔细修改设计。

      【讨论】:

        【解决方案6】:

        当我将Swift 协议导入Objective C 类时,这发生在我身上。对我有帮助的是将*-Swift.h 文件添加到Objective C 类文件中:

        #import "MyApp-Swift.h"
        

        并将协议声明为@objc:

        @objc protocol VerificationGate
        

        【讨论】:

        • 我很好奇结果如何。我有同样的警告。由于尚未生成,因此无法在 Objective-C 标头中导入自动生成的 Swift 桥接头标头。它只能在 Objective-C 实现文件中导入。但是协议一致性是在 Objective-C 头文件中定义的。 Objective-C 标头中的前向声明没有帮助。
        【解决方案7】:

        您必须先定义协议。像下面这样试试

        @protocol LeveyPopListViewDelegate <NSObject>
        //the definition for LeveyPopListViewDelegate
        @end
        
        @interface LeveyPopListView : UIView <LeveyPopListViewDelegate,UITableViewDataSource,   UITableViewDelegate,UITextFieldDelegate>
        
        //the content of LeveyPopListView
        
        @end
        

        【讨论】:

        • 但是我想在协议 LeveyPopListViewDelegate 中使用 LeveyPopListView
        • 可以在协议定义上方转发声明LeveyPopListView。即@class LeveyPopListView;
        • 我明白了。非常感谢。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-08
        • 1970-01-01
        • 1970-01-01
        • 2011-09-20
        相关资源
        最近更新 更多