用于解析继承委托的不兼容属性类型的语法 [英] Syntax for resolving incompatible property type on inherited delegate

查看:141
本文介绍了用于解析继承委托的不兼容属性类型的语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承的一些代码有一个恼人的警告。它声明一个协议,然后使用它来指定委托

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

使用 myTextField 实现 MyTextFieldDelegate ,并使用以下代码调用:

Classes which use myTextField implement the MyTextFieldDelegate and are called it with this code:

if ([delegate respondsToSelector:@selector(myTextFieldSomethingHappened:)])
{
    [delegate myTextFieldSomethingHappened:self];
}

这样做可以创建(合法)警告:警告:属性类型'id'与继承自'UITextField'的类型'id'不兼容

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. 删除该属性。这个工作,但是我得到警告' - myTextFieldSomethingHappened:'在协议中找不到

  2. 完全删除协议。没有警告,但如果您忘记在代理中执行协议,您也会丢失语义警告。

有没有办法定义代理属性使编译器很开心?

Is there a way to define the delegate property such that the compiler is happy?

推荐答案

尝试:

@property (nonatomic, assign) id<UITextFieldDelegate,MyTextFieldDelegate> delegate;

这篇关于用于解析继承委托的不兼容属性类型的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆