manu
manu

Reputation: 1017

Import two objective-c protocols in each others file ends with compiler errors

there are two protocols, each in its own file:

// PMAService.h
#import <Foundation/Foundation.h>
#import "PMAPost.h"
#import "PMAServiceProcessingDelegate.h"

@protocol PMAService <NSObject>

-(void)setupService;
-(BOOL)processPost:(PMAPost *)post withDelegate:(id<PMAServiceProcessingDelegate>)delegate;

@end

// PMAServiceProcessingDelegate.h
#import <Foundation/Foundation.h>
#import "PMAPost.h"
#import "PMAService.h"

@protocol PMAServiceProcessingDelegate <NSObject>

-(void)successfullyProcessedPost:(PMAPost *)post by:(id<PMAService>)service;
-(void)notProcessedPost:(PMAPost *)post by:(id<PMAService>)service withError:(NSError *)error;

@end

each of the protocols needs the opposite for a method declaration. as soon as i create the import in each of the files, the compiler is not able to compile anymore since it tells me that it cannot find one of the protocols.

error messages for PMAService.h (for the #import statement of PMAServiceProcessingDelegate.h)

error messages for PMAServiceProcessingDelegate.h (one for each method declaration):

is there something i missed out? isn't it allowed to import protocols like this?

Upvotes: 3

Views: 1703

Answers (1)

albertamg
albertamg

Reputation: 28572

You have a circular dependency that you can solve using a forward declaration:

// PMAService.h
#import <Foundation/Foundation.h>
#import "PMAPost.h"

@protocol PMAServiceProcessingDelegate;

@protocol PMAService <NSObject>

-(void)setupService;
-(BOOL)processPost:(PMAPost *)post withDelegate:(id<PMAServiceProcessingDelegate>)delegate;

@end

Upvotes: 8

Related Questions