Search code examples
iosxcodedelegateswarningsuitextviewdelegate

Having an ivar 'delegate' in a subclass whose superclass also has same named ivar


I have a subclass defined like this

@protocol UMTextViewDelegate;

@interface UMTexView : UITextView <UITextViewDelegate> {

}

@property (nonatomic, assign) id<UMTextViewDelegate> delegate;

@end

@protocol UMTextViewDelegate <NSObject>
@optional
- (void)textViewDidSelectWordToDefine:(UMTexView*)textView;
@end

But I get a warning Property type 'id<UMTextViewDelegate>' is incompatible with type 'id<UITextViewDelegate>' inherited from 'UITextView'.

How do I suppress this warning ? I tried adding this :

@protocol UMTextViewDelegate <NSObject, UITextViewDelegate>

but no luck. !!

EDIT:

I am not using ARC


Solution

  • The problem you are having is about forward declarations. In the place where you are declaring the delegate, the compiler doesn't know that UMTextViewDelegate descends from UITextViewDelegate. The only thing it knows is that UMTextViewDelegate is a protocol.

    You have to create a forward declaration for the class @class UMTexView;, then put the protocol declaration and then the class declaration.

    On a separate note, it's obvious UMTexView is supposed to be the text delegate for itself. Maybe it would be easier to have UMTexView descending directly from UIView and put an UITextView inside it. Then you wouldn't have any problem with delegate collisions and the UITextViewDelegate would be unaccessible externally.