I have a protocol like this:
#import <Foundation/Foundation.h>
@protocol Prot1 <NSObject>
@required
- (void)methodInProtocol;
@end
This is a protocol for a delegate I want to store in a class like this:
#import <Cocoa/Cocoa.h>
@class Prot1;
@interface Class1 : NSObject
@property (nonatomic, strong) Prot1 *delegate;
- (void)methodInClass;
@end
The implementation for this class is like this:
#import "Class1.h"
#import "Prot1.h"
@implementation Class1
@synthesize delegate;
- (void)methodInClass {
[delegate methodInProt];
}
@end
When I build these pieces of code, I get the following error:
Receiver type 'Prot1' for instance message is a forward declaration
What is wrong here? I did understand that I have to do a forward declaration via @class for the protocol and I thought I only had to #import the protocol, in the class implementation… Isn’t that right?
As it isnt a class, you have to define it as what it is – a protocol 😉
Use forward declaration:
@protocol Prot1;;And use the property like that:
@property (nonatomic, strong) id<Prot1> delegate;