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

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

问题描述

我有两个视图控制器A B ,他们都有彼此作为他们的代表。 p>

当我没有做任何事情,除了在头文件的开头定义协议,并且 #import 另一个的头文件有两个错误 -


找不到BDelegate的协议声明,它显示在
Ah我写了)不能找到
ADelegate的协议声明,它显示在Bh(我写的地方)


在线,人们早些时候写过,头文件的循环包含可能导致问题。他们建议使用 #include @class 声明,如 -

  @class A 

p>

  #import Ah 


$ b b

#import Bh



我已经尝试过几乎每个组合的这些导入, $ c> @classes 和 #include ,但仍然无法摆脱警告。此外,在线解决方案建议将 #import 移动到 .m 文件,但是没有帮助。部分原因是在线解决方案有点模糊 - 如果你可以打破它,这将是巨大的。



有什么建议可以做什么来解决这个问题?






- BigViewController.h -

  #importBaseViewController.h
#includeBaseViewController.h

@class BigViewController;

@protocol BigViewControllerDelegate
- (void)BigViewController:(BigViewController *)bigView;
@end

@interface BigViewController:UIViewController< BaseViewControllerDelegate>
{
//委托
id< BigViewControllerDelegate>代表;

ivars ...
}

@properties ...
@end

------ --------------------------------------------



- BaseViewController.h -

 #< UIKit / UIKit.h> 

#importBigViewController.h
#includeBigViewController.h

@class BigViewController;

@protocol BaseViewControllerDelegate
- (void)setParametersWithItemChosen:(Item *)item;
@end

@interface BaseViewController:UIViewController< ... BigViewControllerDelegate ...>
{

ivars ...

//委托
id< BaseViewControllerDelegate>代表;
}

@properties ...
@end


解决方案

让我进一步减少样本,并标记线条:



VC1.h

  #importVC2.h// A 

@class VC1;

@protocol VC1Delegate // B
@end

@interface VC1:UIViewController< VC2Delegate> // C
@end

VC2.h p>

  #importVC1.h// D 

@class VC2;

@protocol VC2Delegate // E
@end

@interface VC2:UIViewController< VC1Delegate> // F
@end

考虑当#imports VC1.h:到达线A,然后处理导入。行D不执行任何操作,因为VC1.h已导入。然后处理线E.然后行F,我们得到一个错误,因为我们还没有到B行,所以协议没有声明!



然后考虑当事情发生什么#imports VC2 .h:它到达D行,然后处理导入。行A不执行任何操作,因为VC2.h已导入。然后处理线B.然后行C,我们得到一个错误,因为我们没有得到E行,所以协议未声明!



第一步是重新考虑是否这些类真的需要彼此的代理。如果你可以打破周期,这可能是走的路。如果没有,您需要重新构建标头。最直接的方法是将委托放在自己的标头中:



VC1Delegate.h

  @class VC1; 

@protocol VC1Delegate // B
@end

strong> VC1.h

  #importVC1Delegate.h
#importVC2Delegate.h

@interface VC1:UIViewController< VC2Delegate> // C
@end

VC2Delegate.h p>

  @class VC2; 

@protocol VC2Delegate // E
@end

strong> VC2.h

  #importVC1Delegate.h
#importVC2Delegate.h

@interface VC2:UIViewController< VC1Delegate> // F
@end

如果您现在跟踪导入,现在总是在@interface线尝试使用它们之前声明适当的协议。


I have two view controllers A and B, and they both have each other as their delegates.

When I did nothing except define the protocols at the beginning of the header files and #import the other's header file, I got two errors along the lines of -

cannot find protocol declaration for "BDelegate", which was showing in A.h (where I wrote ) cannot find protocol declaration for "ADelegate", which was showing in B.h (where I wrote )

Looking online, people had written earlier that the circular inclusion of header files could be leading to the problems. They recommended either using #include instead, or @class declaration like -

@class A

instead of

#import A.h

inside #import B.h

I have tried almost every combination of these imports, and @classes, and #include but still can't get rid of the warnings. Also, solutions online recommended moving the #import to the .m files but that didn't help either. Part of the reason is that the solutions online are kinda fuzzy - if you could break it down that would be great.

Any suggestions about what can be done to fix this?


-- BigViewController.h --

#import "BaseViewController.h"
#include "BaseViewController.h"

@class BigViewController;

@protocol BigViewControllerDelegate
-(void) BigViewController:(BigViewController *) bigView;
@end

@interface BigViewController : UIViewController <BaseViewControllerDelegate>
{    
     //delegate
     id <BigViewControllerDelegate> delegate;

ivars...    
}

@properties...
@end

--------------------------------------------------

-- BaseViewController.h --

#<UIKit/UIKit.h>

#import "BigViewController.h"
#include "BigViewController.h"

@class BigViewController;

@protocol BaseViewControllerDelegate
- (void) setParametersWithItemChosen:(Item *) item;
@end

@interface BaseViewController : UIViewController <...BigViewControllerDelegate...>
{

   ivars...

   //delegate
    id <BaseViewControllerDelegate> delegate;
}

@properties...
@end

解决方案

Let me reduce the sample even further, and label the lines:

VC1.h

#import "VC2.h"  // A

@class VC1;

@protocol VC1Delegate // B
@end

@interface VC1 : UIViewController <VC2Delegate> // C
@end

VC2.h

#import "VC1.h"  // D

@class VC2;

@protocol VC2Delegate // E
@end

@interface VC2 : UIViewController <VC1Delegate> // F
@end

Consider what happens when something #imports VC1.h: It reaches line A, then the import is processed. Line D does nothing because VC1.h was already imported. Then line E is processed. Then line F, and we get an error because we haven't gotten to line B yet so the protocol is not declared!

Consider then what happens when something #imports VC2.h: It reaches line D, then the import is processed. Line A does nothing because VC2.h was already imported. Then line B is processed. Then line C, and we get an error because we haven't gotten to line E yet so the protocol is not declared!

The first step is to reconsider whether both of these classes really need to be each other's delegates. If you can break the cycle, that would probably be the way to go. If not, you'll need to restructure your headers. The most straightforward way is probably to put the delegates into their own headers:

VC1Delegate.h

@class VC1;

@protocol VC1Delegate // B
@end

VC1.h

#import "VC1Delegate.h"
#import "VC2Delegate.h"

@interface VC1 : UIViewController <VC2Delegate> // C
@end

VC2Delegate.h

@class VC2;

@protocol VC2Delegate // E
@end

VC2.h

#import "VC1Delegate.h"
#import "VC2Delegate.h"

@interface VC2 : UIViewController <VC1Delegate> // F
@end

If you trace through the imports now, you'll see that the appropriate protocols will now always be declared before the @interface lines try to use them.

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

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