9

我有两个相互通信的协议。它们在同一个文件中定义。

@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

协议的类似结构是什么?

4

2 回答 2

13

使用@protocol 进行协议前向声明:

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

@protocol Protocol2 <NSObject>
-(void)protocol:(UIViewController<Protocol1>*)anObject chosenElementAtIndex:(NSInteger)aIndex;
@end
于 2012-06-05T08:12:10.027 回答
1

您的问题是您使用 @class 关键字转发声明的协议。它应该是@protocol。

于 2012-06-05T08:15:05.170 回答