2012-06-05 5 views
7

Ich habe zwei Protokolle, die miteinander kommunizieren. Sie sind in derselben Datei definiert.Protokoll deklarieren wie @class

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

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

Wie ein leeres Protokoll Protocol2 nur zu erklären, zu informieren, Compiler, der es später erklärt wird?

Wenn Protocol2 eine Klasse war, würde ich schreiben @class Protocol2; vor.

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

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

Was ist die ähnliche Konstruktion für Protokolle?

Antwort

10

Verwendung @protocol für Protokolle Forward-Deklaration:

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

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

Das Problem mit dem ist, dass Sie nach vorne Protokoll mit @class Schlüsselwort deklariert haben. Es sollte @ Protokoll sein.

+0

Ich weiß, dass es nicht '@ Klasse' sein sollte. Ich habe einen zweiten Ausschnitt verwendet, um eine Analogie zu Klassen zu zeigen, um die Frage klarer zu machen. Wie auch immer, danke für die Hilfe –

Verwandte Themen