次のようなプロトコルがあります。
#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 するだけでよいと思っていました... そうではないですか?