Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 671
Robert Altman Avatar asked Nov 18 '11 20:11

Robert Altman


3 Answers

try:

@property (nonatomic, assign) id<UITextFieldDelegate,MyTextFieldDelegate> delegate;
like image 122
malhal Avatar answered Nov 15 '22 20:11

malhal


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

like image 27
Max Avatar answered Nov 15 '22 20:11

Max


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
like image 20
lancy Avatar answered Nov 15 '22 20:11

lancy