ios 找不到协议声明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10160887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot find protocol declaration for
提问by Ben Thompson
I have two objects, both of which are view controllers. The first (Ill call it viewController1) declares a protocol. The second (which unsurprisingly I will name viewController2) conforms to this protocol.
我有两个对象,它们都是视图控制器。第一个(我称之为 viewController1)声明了一个协议。第二个(不出所料,我将其命名为 viewController2)符合此协议。
XCode is giving me a build error of: 'Cannot find protocol declaration for viewController1'
XCode 给我一个构建错误:“找不到 viewController1 的协议声明”
I have seen various questions on this subject and I am certain it is to do with a loop error, but I just can't see it in my case...
我已经看到了关于这个主题的各种问题,我确定它与循环错误有关,但在我的情况下我看不到它......
Code below..
下面的代码..
viewController1.h
视图控制器1.h
@protocol viewController1Delegate;
#import "viewController2.h"
@interface viewController1 {
}
@end
@protocol viewController1Delegate <NSObject>
// Some methods
@end
viewController2.h
视图控制器2.h
#import "viewController1.h"
@interface viewController2 <viewController1Delegate> {
}
@end
Initially, I had the import line in viewController1 above that of the protocol declaration. This was preventing the project from building at all. After searching on SO, I realised the problem and switched the two lines around. I am now getting a warning (as opposed to an error). The project builds fine and actually runs perfectly. But I still feel there must be something wrong to be given a warning.
最初,我将 viewController1 中的导入行置于协议声明之上。这完全阻止了该项目的建设。在搜索SO后,我意识到了这个问题并切换了两条线。我现在收到警告(而不是错误)。该项目构建良好,实际上运行完美。但是我还是觉得一定有什么不对的地方才被警告。
Now, as far as I can see, when the compiler gets to viewController1.h, the first thing it sees is the declaration of the protocol. It then imports the viewController.h file and sees this implements this protocol.
现在,据我所知,当编译器到达 viewController1.h 时,它首先看到的是协议的声明。然后它导入 viewController.h 文件并看到它实现了这个协议。
If it were compiling them the other way around, it would look at viewController2.h first, and the first thing it would do is import viewController1.h the first line of which is the protocol declaration.
如果它以相反的方式编译它们,它会首先查看 viewController2.h,它会做的第一件事是导入 viewController1.h,其中第一行是协议声明。
Am I missing something?
我错过了什么吗?
回答by Costique
Remove this line from viewController1.h:
从 中删除此行viewController1.h:
#import "viewController2.h"
The problem is that viewController2's interface is preprocessed before the protocol declaration.
问题是viewController2在协议声明之前对 的接口进行了预处理。
The general structure of the file should be like this:
文件的一般结构应该是这样的:
@protocol viewController1Delegate;
@class viewController2;
@interface viewController1
@end
@protocol viewController1Delegate <NSObject>
@end
回答by xixiaoer007
A.h:
#import "B.h" // A
@class A;
@protocol Delegate_A
(method....)
@end
@interface ViewController : A
@property(nonatomic,strong)id<ViewControllerDelegate> preViewController_B;(protocol A)
@end
B.h:
#import "A.h" // A
@class B;
@protocol Delegate_B
(method....)
@end
@interface ViewController : B
@property(nonatomic,strong)id<ViewControllerDelegate> preViewController_A;(protocol B)
@end
A.m:
@interface A ()<preViewController_B>
@end
@implementation A
(implement protocol....)
end
B.m:
@interface B ()<preViewController_A>
@end
@implementation B
(implement protocol....)
@end
回答by Julius
For those who might need it:
对于那些可能需要它的人:
It's also possible to fix this by moving the importation of ViewController1.hin ViewController2's implementation file (.m) instead of the header file (.h).
也可以通过在ViewController2的实现文件 (.m) 而不是头文件 (.h) 中移动ViewController1.h的导入来解决此问题。
Like so:
像这样:
ViewController1.h
视图控制器1.h
#import ViewController2.h
@interface ViewController1 : UIViewController <ViewController2Delegate>
@end
ViewController2.h
视图控制器2.h
@protocol ViewController2Delegate;
@interface ViewController2
@end
ViewController2.m
视图控制器2.m
#import ViewController2.h
#import ViewController1.h
@implementation ViewController2
@end
This will fix the case where the error happens because ViewController1.his imported in ViewController2.hbefore the protocol declaration.
这将修复发生错误的情况,因为在协议声明之前将ViewController1.h导入到ViewController2.h 中。

