Duplicate protocol definition warning, but I need multiples of this protocol

Go To StackoverFlow.com

5

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:

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?'

2012-04-03 20:59
by Squatch
Without code it's gonna be really hard to do anything useful - CodaFi 2012-04-03 21:01
Implement the protocol in its own header and include where needed - Adam Shiemke 2012-04-03 21:06


7

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

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.

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.

2012-04-03 21:08
by Josh Caswell
Thanks! I totally forgot I could make a header just for the protocol. This is the solution I was looking for - Squatch 2012-04-03 21:11


2

I discovered a similar warning where a @protocol was defined within the header of a class. Breaking that protocol out into its own .h and importing it elsewhere fixed it.

2012-04-10 15:44
by wildcat12
Ads