获得“重复的接口定义”错误,肯定要#import ing头文件 [英] Getting "Duplicate Interface Definition" error, definitely has to #import ing header files

查看:101
本文介绍了获得“重复的接口定义”错误,肯定要#import ing头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在帮助一个iOS项目,其中包含AppDelegate中许多不同类所共有的许多方法和定义。因此,在每个类中,在.h文件中,我使用#importAppDelegate.h。这工作正常,直到我需要访问其中一个已经将AppDelegate导入另一个导入AppDelegate的类的类。此时,我收到了AppDelegate的重复接口定义错误。

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.

好的,这看起来很公平。我已经将AppDelegate导入到我正在导入的文件中,因此AppDelegate正从两个不同的地方导入。所以我删除了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.

但是当我需要导入两个需要导入AppDelegate的类时会发生什么?

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:

我有ClassA.h,ClassB.h和ClassC.h。所有人都有#importAppDelegate.h。当我需要在ClassA中使用#importClassB.h时,我从ClassA中删除#importAppDelegate.h行。一切顺利。但是如果我还需要#ImportClassC.h进入ClassA,但是ClassB和ClassC需要#importAppDelegate.h会发生什么?

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

编辑:

我在一个干净的项目中尝试了上面描述的确切场景,它构建得很好,所以还有其他的东西在起作用。但我可以肯定地说,当这个问题出现在此项目之前时,它是AppDelegate的重复接口定义,当我删除#importAppDelegate.h行时,错误就消失了,我仍然可以通过其他导入文件访问AppDelegate.h方法和枚举。

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.

推荐答案

最好的预防和治疗方法是遵循关于何时从头文件导入的一些指导原则。作为一般规则,永远不要从Objective-C标头导入,除非在这些情况下:

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. 您需要扩展在另一个标头中声明的类。

  2. 您需要声明符合另一个
    标头中声明的协议。

  3. 您需要引用非类,在公共方法和/或属性中的另一个头中定义的非协议类型。要引用协议和类,请使用 @class @protocol 转发声明它们,例如 @class ClassFromOtherHeader;

  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;

其他 #import 应该进入你的实施。我的建议是根据这些规则开始将所有 #import 语句从标题中移出到实现文件中。从您认为位于问题根源的文件开始,然后向外移动。这将解决您的问题,并为您提供更清晰的代码和更快的构建时间的附带好处。

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.

这篇关于获得“重复的接口定义”错误,肯定要#import ing头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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