6

How should I respond to this warning?

warning: duplicate protocol definition of '...' is ignored

My protocol declaration is in its own .h file, and it is #import'ed in a few other files in my project.

Well, just in case, here's the entire header file with the protocol declaration:

#import <Foundation/Foundation.h> 

@class Wrapper;

@protocol WrapperDelegate

@required
- (void)wrapper:(Wrapper *)wrapper didRetrieveData:(NSData *)data;

@optional
- (void)wrapperHasBadCredentials:(Wrapper *)wrapper;
- (void)wrapper:(Wrapper *)wrapper didCreateResourceAtURL:(NSString *)url;
- (void)wrapper:(Wrapper *)wrapper didFailWithError:(NSError *)error;
- (void)wrapper:(Wrapper *)wrapper didReceiveStatusCode:(int)statusCode;

@end

Thanks for any advice.

arrowd
  • 33,231
  • 8
  • 79
  • 110
Elliot
  • 6,086
  • 11
  • 45
  • 57
  • 8
    1. Are you sure that you're `#import`ing it and not `#include`ing it by accident? 2. Are you sure (worth asking) that nothing else in your project uses the same name (WrapperDelegate)? – Ben Zotto Nov 15 '11 at 15:26
  • Quixoto's comment looks suspiciously like an answer :) I have had the same issue where I mistakenly used #include. #import is smart enough to avoid duplication. – Jacob Jennings Nov 15 '11 at 18:56

4 Answers4

6

Check to make sure you don't have the header file added in your project twice, or two different files that both implement the protocol. This is what caused the warning to appear for me.

Elliot
  • 6,086
  • 11
  • 45
  • 57
  • 5
    What happens if i have two class that implement the same protocol? both of them need it, so how do i get around that? – zach Feb 03 '12 at 04:27
  • 2
    @zach (or anybody else with the same problem) - I ran into this issue and posted a question, which has now been answered [here](http://stackoverflow.com/questions/10001293/duplicate-protocol-definition-warning-but-i-need-multiples-of-this-protocol/10001416#10001416). – Squatch Apr 03 '12 at 21:13
3

Yes I had the same problem. I was unable to find the duplicate header file in Xcode project. But when i went through the finder from the Xcode i found the 2 header file of the protocol in the project. Deleting one solved the problem. Thanks.

1

Just to supplement the existing answers here with the specific problem I encountered.

Basically, the compiler isn't lying. It is finding more than one definition of a class, protocol, enum, define, or whatever exists in the offending header files.

The fault could be a combination of your header files and the header search path.

At first, the issues seem puzzling as we know that the statement #import will only import files that have not already been implemented. Therefore, unlike #include, this problem shouldn't happen, right?

#import does work. However, if your headers have been set up incorrectly then although it may encounter a file with the same name, e.g. MyLibrary.h, if that file exists in two different places both of which are in the header search path then Xcode will perceive those as two different files.

In my case, I had a static library build phase which copied public headers.

enter image description here

The dependent products searched the folder above - defined in Build Settings as include/$(TARGET_NAME) - and the source folder of my project.

That meant two different folders - both in the header search path - that contained the file MyLibrary.h. Everything in that file would cause a duplicate or redefinition compiler warning or linker error.

TLDR: the same file may be in two different folders and both are in your header search path. Check your paths, and if you've incorporated a static library into the project or workspace, also check where the public headers are copied to as part of your investigation.

Max MacLeod
  • 26,115
  • 13
  • 104
  • 132
0

Please make sure you have used

#import

rather than

#include

Find the difference here.

Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256