解决继承委托上不兼容的属性类型的语法

发布于 2024-12-16 13:39:42 字数 844 浏览 4 评论 0 原文

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

@protocol MyTextFieldDelegate;

@interface MyTextField: UITextField
@property (nonatomic, assign) id<MyTextFieldDelegate> delegate;
@end

@protocol MyTextFieldDelegate <UITextFieldDelegate>
@optional
- (void)myTextFieldSomethingHappened:(MyTextField *)textField;
@end

使用 myTextField 的委托类实现 MyTextFieldDelegate 并使用以下代码调用它:

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

这有效,但创建了(合法的)警告:警告:属性类型“id”与从“UITextField”继承的类型“id”不兼容

以下是我提出的解决方案:

  1. 删除该属性。这有效,但我收到警告'-myTextFieldSomethingHappened:'在协议中找不到
  2. 完全删除协议。没有警告,但如果您忘记在委托中实现协议,您也会丢失语义警告。

有没有办法定义委托属性以使编译器满意?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夕色琉璃 2024-12-23 13:39:43

尝试:

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

try:

@property (nonatomic, assign) id<UITextFieldDelegate,MyTextFieldDelegate> delegate;
寄离 2024-12-23 13:39:43

UITextField 还具有名为 委托,但它有另一种类型。只需将您的 delegate 属性重命名为其他名称即可。

UITextField has also got property named delegate, but it has another type. Just rename your delegate property to something else.

等待我真够勒 2024-12-23 13:39:43

在 UITableView.h 中找到了答案。

UIScrollView 有属性名称 delegate,UITableView 有相同名称的属性。

@protocol UITableViewDelegate<NSObject, UIScrollViewDelegate>
// Your code
......

@end

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
彼岸花似海 2024-12-23 13:39:43

最初的问题是在声明 delegate 属性时没有有关 MyTextFieldDelegate 继承的信息。这是由协议的前向声明(@protocol MyTextFieldDelegate;)引起的。

我遇到了同样的问题,但在另一个 .h 文件中声明了协议。就我而言,解决方案只是 #import 适当的标头。

在你的情况下,你只需要交换声明的顺序:

@class MyTextField;

@protocol MyTextFieldDelegate <UITextFieldDelegate>
@optional
- (void)myTextFieldSomethingHappened:(MyTextField *)textField;
@end

@interface MyTextField : UITextField
@property (nonatomic, assign) id <MyTextFieldDelegate> delegate;
@end

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文