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:
- Remove the property. This works but I get the warning '-myTextFieldSomethingHappened:' not found in protocol(s)
- 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?