我有一个这样的协议:

#import <Foundation/Foundation.h>

@protocol Prot1 <NSObject>

@required
- (void)methodInProtocol;

@end


这是我要存储在这样的类中的委托的协议:

#import <Cocoa/Cocoa.h>

@class Prot1;

@interface Class1 : NSObject

@property (nonatomic, strong) Prot1 *delegate;

- (void)methodInClass;

@end


此类的实现如下:

#import "Class1.h"
#import "Prot1.h"

@implementation Class1

@synthesize delegate;

- (void)methodInClass {
    [delegate methodInProt];
}

@end


构建这些代码段时,出现以下错误:

Receiver type 'Prot1' for instance message is a forward declaration


怎么了我确实理解我必须通过@class对协议进行前向声明,并且我认为我只需要在类实现中#import协议即可。不是吗?

最佳答案

由于它不是类,因此必须将其定义为类-协议;)

使用向前声明:@protocol Prot1;;

并使用如下属性:
@property (nonatomic, strong) id<Prot1> delegate;

09-04 16:04