2012-06-05 34 views
7

Tôi có hai giao thức giao tiếp với nhau. Chúng được định nghĩa trong cùng một tệp.Khai báo giao thức như @class

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

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

Làm cách nào để khai báo giao thức trống Protocol2 chỉ để biết trình biên dịch được khai báo sau?

Nếu Protocol2 là lớp tôi muốn viết @class Protocol2; trước đó.

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

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

Cấu trúc tương tự cho giao thức là gì?

Trả lời

10

Sử dụng @protocol cho giao thức phía trước tuyên bố:

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

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

Vấn đề với bạn là bạn đã chuyển tiếp tuyên bố giao thức với từ khóa @class. Nó phải là @protocol.

+0

Tôi biết rằng nó không phải là '@ class'. Tôi đã sử dụng đoạn mã thứ hai để hiển thị tương tự với Lớp học, để làm cho câu hỏi rõ ràng hơn. Dù sao, cảm ơn sự giúp đỡ –

Các vấn đề liên quan