问题
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?
回答1:
try:
@property (nonatomic, assign) id<UITextFieldDelegate,MyTextFieldDelegate> delegate;
回答2:
UITextField has also got property named delegate, but it has another type. Just rename your delegate
property to something else.
回答3:
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
回答4:
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
来源:https://stackoverflow.com/questions/8188707/syntax-for-resolving-incompatible-property-type-on-inherited-delegate