21
votes

I'm helping on an iOS project with lots of methods and definitions common to many different classes in the AppDelegate. So, in each of those classes, in the .h file, I use #import "AppDelegate.h". This works fine until I need access to one of those classes that already imports the AppDelegate into another class that imports AppDelegate. At this point, I get a Duplicate Interface Definition error for AppDelegate.

Ok, so that seems fair. I'm already importing AppDelegate into a file that I'm importing, so AppDelegate is getting imported from two different places. So I remove the AppDelegate line, and everything is fine.

But what happens when I need to import two classes that both need to import AppDelegate?

I have a very specific problem that I'm trying to wrap my head around, and I know it is being caused by something that has to do with this, but I'm not sure what. So I'm hoping if I figure out how I'm supposed to be handling this sort of importing, and sort everything else out, and hope that this solves my problem. So to put this in more concrete terms:

I have ClassA.h, ClassB.h, and ClassC.h. All have #import "AppDelegate.h". When I need to use #import "ClassB.h" in ClassA, I remove the #import "AppDelegate.h" line from ClassA. Everything works smoothly. But what happens if I also need to #import "ClassC.h" into ClassA, and but ClassB and ClassC NEED to have the #import "AppDelegate.h"?

EDIT:

I tried the exact scenario I described above in a clean project, and it built fine, so there is something else at play. But what I can say with certainty is that when this issue came up previously with this project, it was a duplicate interface definition of AppDelegate, and when I removed the #import "AppDelegate.h" line, the error went away, and I still had access to the AppDelegate.h methods and enums through other imported files.

6
Are you sure you use #import and not #include ? #import is designed to never import the same header file twice, so it does not seem logical that the compiler #import "AppDelegate.h" twiceAliSoftware
100% sure. My google research has said the same thing. I've had a similar issue in the past, which I was able to fix by including the file in the .m instead of the .h, but unfortunately, I need access to an enum in the imported file in the importing .h, so this is not an option.CowGoes
But I just made a sample app with exactly the scenario I described at the bottom, and it worked. So I guess there is something else at play here. But what I can say with certainty is that when this issue came up in the past, it was a duplicate interface definition of AppDelegate, and when I removed the #import "AppDelegate.h" line, the error went away, and I still had access to the AppDelegate.h methods and enums through other imported files.CowGoes
Write enum in separate *.h file and import them where you need.ZGl6YXNt
But I also need to use methods in the AppDelegate. Should I import the AppDelegate in the .m file then?CowGoes

6 Answers

29
votes

The best prevention and cure for this is to follow some guidelines on when to import from a header file. As a general rule, never import from an Objective-C header except in these cases:

  1. You need to extend a class declared in another header.
  2. You need to declare conformity with a protocol declared in another header.
  3. You need to refer to a non-class, non-protocol type defined in another header in the public methods and / or properties. To refer to protocols and classes, forward declare them with @class or @protocol, like @class ClassFromOtherHeader;

Every other #import should go in your implementation. My recommendation is to start moving all your #import statements out of headers and into the implementation files according to these rules. Start with the files you think are at the root of the problem and move outward. This will fix your problem and give you the side benefit of clearer code and faster build times.

21
votes

For me none of the above answers were helping, nor did the answer given here work.

What fixed it for me was closing Xcode, going to ~/Library/Developer/Xcode/DerivedData and deleting all of the derived data associated with this project. After that I reopened the project and it was working fine.

Hope that helps someone!

3
votes

In my case, none of the solutions mentioned fixed the issue. Xcode was reporting a duplicate interface for a class I rewrote in Swift. Somehow it kept pulling in the Objective-C header file for a class that wasn't directly referenced in the project.

I opened the Terminal, cd into the project directory, then ran the following to track down any files that were including the class header: grep -nr ProblemClassName.h .

It turned out that the bridging header included an obsolete file that wasn't even referenced in the project navigator. That in turn was importing the header files referenced in the Xcode error, that were also not included in the Xcode project navigator. Now I know to not rely only on the Xcode project navigator for files referenced by the error.

tl;dr Double check the bridging header to ensure that all files that are imported there should be there and are not importing headers that are in-turn importing the problem headers.

1
votes

I found that a project had a subproject and instead of referencing the includes in the subproject with the proper syntax:

#import <SubProject/Filename.h>

It was directly importing them

#import <Filename.h>

This was only possible because the path of the subproject was included in the "header search paths" of the main project - which is the wrong way to do business. So I deleted it from there. The subproject should copy the needed included files in its "build phases - copy files" section (which was already happening actually), and the proper form of import that uses the Subproject/Filename.h syntax should be used.

0
votes

Fwiw I started getting this seemingly at random - for me the fix was to do Product->Clean and it magically went away.

0
votes

For me, I forgot to include parenthesis in interface definition in m file.