I have a protocolA in its own header file, that serves as an interface.
Then I have two concrete classes that are different implementations of that protocol.
Now, in my ViewController, I use this for declaring a property of type id , and it allows me to swap implementations without VC knowing anything about this.
I also have protocolB, that serves for delegate calls from those two implementation objects to the VC.
The problem is I have circular dependency.
In protocol A, i need to declare a “delegate” property of type id.
In protocol B, the delegate methods are sending reference to the caller which is an object of type id…
Is there a better design?
UPDATE with example, maybe it will be helpful for others.
ProtocolA header:
#import <Foundation/Foundation.h>
/*!
Abstract interface for any Provider
*/
@protocol DataProviderDatasource <NSObject>
@required
@property (nonatomic) id <DataProviderDelegate> delegate;
-(void)update;
@end
ProtocolB header
#import <Foundation/Foundation.h>
#import "DataProviderDatasource.h"
/*!
Protocol that each Data Provider implements to make delegate calls to notify its delegate about data management operations.
*/
@protocol DataProviderDelegate <NSObject>
-(void)dataProviderWillUpdate:(id<DataProviderDatasource>)dataProvider;
-(void)dataProviderdidUpdate:(id<DataProviderDatasource>)dataProvider;
@end
like classes, you can forward declare protocols:
then the compiler won’t barf when it sees the protocol name and circular dependence is broken (unless your protocols both derive from another which is of course silly).
you can then
#importMONProtocolB wherever there is physical dependence.