Robert Altman
Robert Altman

Reputation: 5525

Syntax for resolving incompatible property type on inherited delegate

Some code I inherited has an annoying warning. It declares a protocol and then uses that to specify the delegate

@protocol MyTextFieldDelegate;

@interface MyTextField: UITextField
@property (nonatomic, assign) id<MyTextFieldDelegate> delegate;
@end

@protocol MyTextFieldDelegate <UITextFieldDelegate>
@optional
- (void)myTextFieldSomethingHappened:(MyTextField *)textField;
@end

Classes which use myTextField implement the MyTextFieldDelegate and are called it with this code:

if ([delegate respondsToSelector:@selector(myTextFieldSomethingHappened:)])
{
    [delegate myTextFieldSomethingHappened:self];
}

This works, but creates the (legitimate) warning: warning: property type 'id' is incompatible with type 'id' inherited from 'UITextField'

Here are the solutions I've come up with:

  1. Remove the property. This works but I get the warning '-myTextFieldSomethingHappened:' not found in protocol(s)
  2. Drop the protocol entirely. No warnings, but you also lose the semantic warnings if you forget to implement the protocol in the delegate.

Is there a way to define the delegate property such that the compiler is happy?

Upvotes: 11

Views: 5248

Answers (4)

still
still

Reputation: 21

The original problem is that there is no information about MyTextFieldDelegate's inheritance during declaration of delegate property. It's caused by forward declaration of protocol (@protocol MyTextFieldDelegate;).

I've faced the same problem but with protocol declaration in the other .h file. In my case solution was just to #import appropriate header.

In your case you just need to swap the order of declaration:

@class MyTextField;

@protocol MyTextFieldDelegate <UITextFieldDelegate>
@optional
- (void)myTextFieldSomethingHappened:(MyTextField *)textField;
@end

@interface MyTextField : UITextField
@property (nonatomic, assign) id <MyTextFieldDelegate> delegate;
@end

Upvotes: 2

lancy
lancy

Reputation: 857

Found the answer in UITableView.h.

The UIScrollView has property name delegate, and the UITableView has the same name property.

@protocol UITableViewDelegate<NSObject, UIScrollViewDelegate>
// Your code
......

@end

Upvotes: 4

malhal
malhal

Reputation: 30746

try:

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

Upvotes: 31

Max
Max

Reputation: 16709

UITextField has also got property named delegate, but it has another type. Just rename your delegate property to something else.

Upvotes: 5

Related Questions