2

次のようなプロトコルがあります。

#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 するだけでよいと思っていました... そうではないですか?

4

1 に答える 1

6

クラスではないので、プロトコルとして定義する必要があります;)

前方宣言を使用します: @protocol Prot1;;

そして、そのようなプロパティを使用します:
@property (nonatomic, strong) id<Prot1> delegate;

于 2012-04-24T11:21:31.700 に答える