Reputation: 3959
I have two files defining protocols, for implementing the observer pattern, and I'm getting a compiler error from one of them.
// ObserverDelegate.h
#import <Foundation/Foundation.h>
@protocol ObserverDelegate <NSObject>
@required
@end
// ObservableDelegate.h
#import <Foundation/Foundation.h>
#import "ObserverDelegate.h"
@protocol ObservableDelegate <NSObject>
@required
-(void) addObserver: (ObservableDelegate*) observer; // ERROR ON THIS LINE WHEN COMPILE
@end
I only have .h files for ObserverDelegate
and ObservableDelegate
, there are no corresponding .m files.
The error says "expected a type" in ObserveableDelegate.h on the line -(void) addObserver: (ObservableDelegate*) observer;
Upvotes: 1
Views: 3099
Reputation: 64002
ObservableDelegate
isn't a type, it's the name of the protocol. You can't use it as the type of a method parameter. If you want to require that the argument to the method conform to that protocol, you express it like this:
- (void)addObserver: (id<ObservableDelegate>)observer;
This says that the method takes any object (id
is the "generic" pointer) which conforms to ObservableDelegate
. You can use a more specific type than id
if you want and you know, for example, that the delegate should always be a UIViewController
subclass as well.
Upvotes: 8