3

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)

  • 'PMAServiceProcessingDelegate.h' file not found

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

  • Cannot find declaration for 'PMAService'
  • Cannot find declaration for 'PMAService'

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

manu
  • 967
  • 1
  • 11
  • 28
  • This may sound like a dumb question but might be relevant: are your two protocol .h files included in the list of files in your project? In other words, can they can be seen on the left side of your Xcode window, with all the other .m & .h files? Also, [a potentially helpful question previously asked](http://stackoverflow.com/questions/2737148/cannot-find-protocol-declaration-in-xcode). – Michael Dautermann Jan 08 '12 at 14:02
  • hi micheal. yeah, they are. albertamg below already solved my problem with a forward declaration... thanks anyway :) – manu Jan 08 '12 at 14:08

1 Answers1

8

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
albertamg
  • 28,492
  • 6
  • 64
  • 71
  • and thats all? i only have to do this on one of the files? ... strange, it really works :-) never saw a concept like this. thank you! – manu Jan 08 '12 at 14:06