2012-04-24 110 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;