2012-04-24 100 views
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協議...是不是正確?

回答

6

由於它不是一類,你必須把它定義爲它是什麼 - 一個協議;)

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

而且使用這樣的屬性:
@property (nonatomic, strong) id<Prot1> delegate;