George WS
George WS

Reputation: 4093

Declare protocol conformance in objective-c protocol forward-declaration

Assume a class has been defined like this in OriginalObject.h:

@protocol OriginalDelegate;

@interface OriginalObject : NSObject  {}
@property (nullable, nonatomic, weak) id<OriginalDelegate> delegate;
@end

@protocol OriginalDelegate <NSObject>
// … delegate method declarations …
@end

Now, in ExtendedObject.h, I want to do this:

#import "OriginalObject.h"

@protocol ExtendedDelegate;

@interface ExtendedObject : OriginalObject {}
@property (nullable, nonatomic, weak) id<ExtendedDelegate> delegate;
@end

@protocol ExtendedDelegate <OriginalDelegate>
// … additional delegate method declarations …
@end

Attempting this gives me the following warning on the @property … delegate; line of ExtendedObject.h:

Property type 'id<ExtendedDelegate> _Nullable' is incompatible with type 'id<OriginalDelegate> _Nullable' inherited from 'OriginalObject'

It appears that the compiler doesn't know ExtendedDelegate will conform to OriginalDelegate. Moving the full protocol declaration above the ExtendedObject interface in ExtendedObject.h resolves the warning:

#import "OriginalObject.h"

@protocol ExtendedDelegate <OriginalDelegate>
// … additional delegate method declarations …
@end

@interface ExtendedObject : OriginalObject {}
@property (nullable, nonatomic, weak) id<ExtendedDelegate> delegate;
@end

What I'd like to know is… is there any way to tell the compiler in the forward-declaration that ExtendedDelegate will conform to OriginalDelegate (enabling use of something more like the first version of ExtendedObject.h above)?


Neither of the following attempts at such a forward-declaration seem to be valid syntax:

@protocol ExtendedDelegate <OriginalDelegate>;
@protocol ExtendedDelegate : OriginalDelegate;

Upvotes: 0

Views: 140

Answers (0)

Related Questions