【问题标题】:Forward declaration error前向声明错误
【发布时间】:2012-04-24 11:14:36
【问题描述】:

我有这样的协议:

#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 协议......不是吗?

【问题讨论】:

    标签: objective-c protocols forward-declaration


    【解决方案1】:

    因为它不是一个类,所以你必须将它定义为它是什么 - 一个协议;)

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

    然后像这样使用属性:
    @property (nonatomic, strong) id&lt;Prot1&gt; delegate;

    【讨论】:

      猜你喜欢
      • 2011-10-26
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-13
      • 1970-01-01
      相关资源
      最近更新 更多