2012-06-05 67 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進行類比,以使問題更清晰。無論如何,感謝您的幫助 –