Programing

프로토콜 선언을 찾을 수 없습니다.

lottogame 2020. 12. 6. 20:51
반응형

프로토콜 선언을 찾을 수 없습니다.


두 개의 및가 view controllers A있고 B둘 다 서로를 대리인으로 지정합니다.

헤더 파일과 #import다른 헤더 파일의 시작 부분에 프로토콜을 정의하는 것 외에 아무것도하지 않았을 때, 다음 같은 두 가지 오류가 발생했습니다.

Ah (내가 쓴 곳)에 나타난 "BDelegate"에 대한 프로토콜 선언을 찾을 수 없습니다. Bh (내가 쓴 곳)에 나타난 "ADelegate"에 대한 프로토콜 선언을 찾을 수 없습니다.

온라인에서 사람들은 헤더 파일의 순환 포함이 문제를 일으킬 수 있다고 이전에 썼습니다. #include대신 사용 하거나 @class다음과 같은 선언을 권장합니다.

@class A

대신에

#import A.h

내부 #import B.h

나는이 수입의 거의 모든 조합을 시도하고 @classes, 그리고 #include여전히 경고를 제거 할 수 없습니다. 또한, 솔루션은 온라인으로 이동 추천 #import받는 .m파일하지만, 도움이 중 하나를하지 않았다. 그 이유 중 하나는 온라인 솔루션이 다소 모호하기 때문입니다. 분해 할 수 있다면 좋을 것입니다.

이 문제를 해결하기 위해 수행 할 수있는 제안 사항이 있습니까?


-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

샘플을 더 줄이고 라인에 레이블을 지정하겠습니다.

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

#imports VC1.h : A 행에 도달하면 가져 오기가 처리됩니다. VC1.h가 이미 임포트 되었기 때문에 라인 D는 아무것도하지 않습니다. 그런 다음 라인 E가 처리됩니다. 그런 다음 F 행, 아직 B 행에 도달하지 않았기 때문에 프로토콜이 선언되지 않았기 때문에 오류가 발생합니다!

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.


Write protocol declaration code above the #import lines e.g.

@protocol -----

@end

import ----

@interface classname ---


I had almost the same problem, and I fixed it thanks to the answer above, but in a slightly different way.

all I did was to put the #import line after the protocol declaration in the header file. hope I can help. and if anyone know that this is bad programing for some reason, pls let me know


I found another solution to this issue because I didn't really like the idea of just having some #imports in between the class and the protocol declaration.

Basically you just move <YourProtocolName> from the .h class file to the class extension in the .m file

So in your .m file you add

@interface YourClassName () <YourProtocolName>
@end

I don't know if this is really a good practise but it looks like a cleaner solution to avoid the import cycles.


Try putting the < BaseViewControllerDelegate > or < BigViewControllerDelegate > in implementation file rather then header file. It will work.


I followed the fix of moving the protocol before the import and it fixed the problem... the import included the delegate, so that was causing the problem.

But then I thought, why was I importing the delegate anyway? I wasn't referencing its properties and I wasn't calling any of its methods directly (that's what the protocol declare was for).

I tried commenting out the import of the delegate and saw where the error came up and found that what I was importing when I was importing the delegate was actually a declaration that the delegate was importing i.e. I was importing A (also my delegate), A was importing B, what I was actually using was B. So I left the import of A commented out and added an import for B. Then I could put the import-protocol order back the way it was.

참고URL : https://stackoverflow.com/questions/6447573/cannot-find-protocol-declaration

반응형