2012-06-05 59 views
7

我有两个协议相互通信。它们在相同的文件中定义。声明协议,如@class

@protocol Protocol1 <NSObject> 
-(void)setProtocolDelegate:(id<Protocol2>)delegate; 
@end 

@protocol Protocol2 <NSObject> 
-(void)protocol:(UIViewController<Protocol1>*)anObject chosenElementAtIndex:(NSInteger)aIndex; 
@end 

如何声明一个空的协议Protocol2只是为了让知道它以后宣布编译器?

如果Protocol2是我以前写的@class Protocol2;

@class Protocol2; 
@protocol Protocol1 <NSObject> 
-(void)setProtocolDelegate:(Protocol2*)delegate; 
@end 

@interface Protocol2 <NSObject> 
-(void)protocol:(UIViewController<Protocol1>*)anObject chosenElementAtIndex:(NSInteger)aIndex; 
@end 

协议的类似结构是什么?

回答

10

的协议使用@protocol向前声明:

@protocol Protocol2; 
@protocol Protocol1 <NSObject> 
-(void)setProtocolDelegate:(id<Protocol2>)delegate; 
@end 

@protocol Protocol2 <NSObject> 
-(void)protocol:(UIViewController<Protocol1>*)anObject chosenElementAtIndex:(NSInteger)aIndex; 
@end 
1

的问题与你的是你前进宣布与@class关键字协议。 它应该是@protocol。

+0

我知道它不应该是'@ class'。我用第二个片段与Classes进行类比,以使问题更清晰。无论如何,感谢您的帮助 –