前向声明错误

der*_*oni 2 protocols objective-c forward-declaration

我有这样的协议:

#import <Foundation/Foundation.h>

@protocol Prot1 <NSObject>

@required
- (void)methodInProtocol;

@end
Run Code Online (Sandbox Code Playgroud)

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

#import <Cocoa/Cocoa.h>

@class Prot1;

@interface Class1 : NSObject

@property (nonatomic, strong) Prot1 *delegate;

- (void)methodInClass;

@end
Run Code Online (Sandbox Code Playgroud)

这个类的实现是这样的:

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

@implementation Class1

@synthesize delegate;

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

@end
Run Code Online (Sandbox Code Playgroud)

当我构建这些代码时,我收到以下错误:

Receiver type 'Prot1' for instance message is a forward declaration
Run Code Online (Sandbox Code Playgroud)

这有什么不对?我确实理解我必须通过@class为协议做一个前向声明,我认为我只需要#import协议,在类实现中......不是吗?

cal*_*kus 6

因为它不是一个类,你必须将其定义为它是一个协议;)

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

并使用这样的属性:
@property (nonatomic, strong) id<Prot1> delegate;