协议定义警告重复,但我需要此协议的倍数 [英] Duplicate protocol definition warning, but I need multiples of this protocol

查看:104
本文介绍了协议定义警告重复,但我需要此协议的倍数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:这类似于此问题,但这是不一样.我保证.

Note: This is similar to this question but it is not the same. I promise.

我有一系列的表格视图,它们需要一个模式视图来对表格的内容进行排序.为此,我在一个表视图控制器的头文件中设置了一个简单的协议,效果很好.然后,我将此协议复制到其他表视图控制器中,并收到以下警告:

I have a series of table views that call upon a modal view for sorting the table's contents. To do this, I set up a simple protocol in one table view controller's header file and it worked great. I then copied this protocol over to my other table view controllers and got this warning:

"ModalViewDelegate"的重复协议定义将被忽略

Duplicate protocol definition of 'ModalViewDelegate' is ignored

现在我意识到这只是一个警告,但是我宁愿每次编译都看不到它.为了消除警告,我导入了最初在其中定义协议的头文件.再一次,我并不完全满意.将头文件导入每个表视图似乎很草率,因此我可以在没有警告的情况下使用该协议.

Now I realize it is just a warning, but I would rather not see it every time I compile. To get rid of the warnings, I imported the header file in which the protocol was originally defined. Once again, I was not completely satisfied. It seems sloppy to import the header file to every table view just so I can use the protocol without warnings.

如果您已阅读本文,谢谢.我的问题是为什么会这样?在仍然使用相同协议的情况下,有没有更好的方法来消除此警告?'

If you have read this far, I thank you. My questions are 'Why is this happening? Is there a better way of getting rid of this warning while still using the same protocol?'

推荐答案

在仍然使用相同协议的情况下,有没有更好的方法来消除此警告?

Is there a better way of getting rid of this warning while still using the same protocol?

编译器需要了解协议,以便您引用它.有两种方法可以实现:将声明协议的标头导入使用协议的文件中,或在这些文件中进行协议的正向声明:@protocol MyProtocol;.第二种方法仅在协议需要相互引用时才有用(避免循环导入).如果一个类需要采用协议,则需要查看协议中方法的声明,这意味着它需要查看协议声明本身,即标头.

The compiler needs to know about the protocol in order for you to refer to it. There's two ways you can make that happen: import the header where the protocol is declared into the files where you're using it, or make a forward declaration of the protocol in those files: @protocol MyProtocol;. The second is really only useful when protocols need to refer to each other (to avoid circular imports); if a class needs to adopt the protocol, it needs to see the declarations of the methods in the protocol, which means it needs to see the protocol declaration itself, i.e., the header.

将头文件导入每个表视图似乎很草率,以便我可以在没有警告的情况下使用该协议.

It seems sloppy to import the header file to every table view just so I can use the protocol without warnings.

这不是马虎,而是事情的方式.听起来对您来说,将协议声明放入其 own 标头并将其导入到任何需要的位置似乎很有意义.

This is not sloppy, it's the way things work. It sounds like it may make sense for you to put the protocol declaration into its own header and import that wherever it's needed.

这篇关于协议定义警告重复,但我需要此协议的倍数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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