Reputation: 5525
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:
Is there a way to define the delegate property such that the compiler is happy?
Upvotes: 11
Views: 5248
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
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
Reputation: 30746
try:
@property (nonatomic, assign) id<UITextFieldDelegate,MyTextFieldDelegate> delegate;
Upvotes: 31