Syntax for resolving incompatible property type on inherited delegate

被刻印的时光 ゝ 提交于 2019-12-22 03:58:16

问题


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?


回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!