找不到协议声明 [英] Cannot find protocol declaration for

查看:115
本文介绍了找不到协议声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个对象,这两个对象都是视图控制器。第一个(Ill调用它viewController1)声明一个协议。第二个(不奇怪的是我将命名viewController2)符合此协议。



XCode给我一个构建错误:'找不到viewController1的协议声明'



我已经看到了关于这个问题的各种问题,我确定这是一个循环错误,但我看不到它在我的情况...



下面的代码



viewController1.h

  @protocol viewController1Delegate; 

#importviewController2.h

@interface viewController1 {

}

@end

@protocol viewController1Delegate< NSObject>

//某些方法

@end

viewController2.h

  #importviewController1.h

@interface viewController2< viewController1Delegate> {

}

@end

,我在viewController1中的导入行高于协议声明。这阻碍了项目建设。在搜索SO之后,我意识到了这个问题,并将两行切换。我现在得到一个警告(而不是一个错误)。该项目建成良好,实际运行完美。但是我仍然觉得有一些错误要给出警告。



现在,据我看到,当编译器到达ViewController1.h时,第一个它看到的是协议的声明。然后导入viewController.h文件,看到这个实现了这个协议。



如果是以相反的方式编译它们,那么首先看看viewController2.h,它将首先做的是导入viewController1.h,其第一行是协议声明。



我是否缺少某些东西?

解决方案

viewController1.h中删除此行

  #importviewController2.h

问题是在协议声明之前, viewController2 的接口被预处理。



文件的一般结构应该是这样的:

  @protocol viewController1Delegate; 
@class viewController2;

@interface viewController1
@end

@protocol viewController1Delegate< NSObject>
@end


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.

XCode is giving me a build error of: 'Cannot find protocol declaration for 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

@protocol viewController1Delegate;

#import "viewController2.h"

@interface viewController1 {

}

@end

@protocol viewController1Delegate <NSObject>

// Some methods

@end

viewController2.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.

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.

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.

Am I missing something?

解决方案

Remove this line from viewController1.h:

#import "viewController2.h"

The problem is that viewController2's interface is preprocessed before the protocol declaration.

The general structure of the file should be like this:

@protocol viewController1Delegate;
@class viewController2;

@interface viewController1
@end

@protocol viewController1Delegate <NSObject>
@end

这篇关于找不到协议声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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