为什么将 NSObject 的协议附加到协议实现上 [英] Why tack a protocol of NSObject to a protocol implementation

查看:27
本文介绍了为什么将 NSObject 的协议附加到协议实现上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在看到一些类似于以下内容的代码:

I have been seeing some code around that resembles the following:

@protocol MyProtocol <NSObject>
// write some methods.
@end

MyProtocol 符合 NSObject 协议有什么特别的原因吗?如果您执行以下操作,那岂不是很多余:

Is there any particular reason why MyProtocol conforms to the NSObject protocol? Isn't that rather redundant in that if you do something such as:

id <MyProtocol> foo; // foo here conforms to NSObject AND MyProtocol?

只是好奇这是什么逻辑.

Just curious what the logic is.

推荐答案

我很确定您这样做的原因是将 NSObject 成员(例如保留和释放)添加到您的协议中.从技术上讲,您仍然可以发送这些消息,但如果没有它,您将收到编译器警告.

I'm pretty sure the reason you would do this is to add the NSObject members (say like retain and release) to your protocol. Technically you can still send those messages anyways but you will get a compiler warning without it.

这篇关于为什么将 NSObject 的协议附加到协议实现上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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