简体   繁体   English

代理错误

[英]Appdelegate error

I'm getting run time error as duplicate interface definition for class app delegate.So what is the wrong with this code. 我收到运行时错误作为类应用程序委托的重复接口定义,所以这段代码有什么问题。

#import <UIKit/UIKit.h>

@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) ViewController *viewController;

@end

At the begin of your header file state: 在头文件状态的开头:

#if !defined APPDELEGATE_H
#define APPDELEGATE_H

and at the end state: 并在结束状态:

#endif

Most probably root cause of this error is that you included AppDelegate.h in some classes header file and .m file. 此错误的根本原因可能是您在某些类头文件和.m文件中包含了AppDelegate.h。 While compiling the .m file the corresponding .h file is included (and probably some other .h files are included). 在编译.m文件时,将包括相应的.h文件(可能还包括其他一些.h文件)。 In any of these .h files AppDelegate.h is included. 在任何这些.h文件中,都包含AppDelegate.h。 Plus you include it in the .m file. 另外,您将其包括在.m文件中。 That will cause a duplicate definition of the interface from the compiler's point of view. 从编译器的角度来看,这将导致接口的重复定义。 The solution above is not really a solution. 上面的解决方案并不是真正的解决方案。 Strictly spoken it is a workaround. 严格来说,这是一种解决方法。 But it is quite standard and apple uses it in all of their templates. 但这是相当标准的,Apple在所有模板中都使用了它。 It's just a workaround because it does not solve the issue but deals with it. 这只是一种解决方法,因为它不能解决问题,但可以解决问题。

Proper solutions would be: In the .h file do not include other .h files if avoidable. 正确的解决方案是:在.h文件中,如果可以避免的话,不要包括其他.h文件。 Use @class statemenst where ever appropriate. 在适当的地方使用@class statemenst。 Never repeat incuding of any .h file in a .m file when the .h file is already included in any of the other included .h files. 当.h文件已包含在任何其他包含的.h文件中时,切勿重复插入.m文件中的任何.h文件。 You may think "this is a pain in the a....". 您可能会认为“这真是太痛苦了……”。 And you are right :) Therefore I suggest to use the common #if !defined XY_H / #define XY_H / #endif pattern, although I believe that this is just a workaround. 您说对了:)因此,尽管我认为这只是一种解决方法,但我建议使用常见的#if !defined XY_H / #define XY_H / #endif模式。

#if !defined APPDELEGATE_H
#define APPDELEGATE_H
#import <UIKit/UIKit.h>

@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) ViewController *viewController;

@end
#endif

I just ran into this problem. 我只是遇到了这个问题。

What I had done was to drag and drop files that had an #import AppDelegate from another project that also contained the exactly named AppDelegate.h/.m class. 我所做的是将包含#import AppDelegate文件从另一个项目中拖放,该项目也包含完全命名的AppDelegate.h / .m类。 When I dropped the files into my project I REFERENCED them from that project instead of COPYING them. 当我将文件拖放到项目中时,我从该项目中引用了它们,而不是复制它们。

By doing so, those files were in conflict which AppDelegate to import and I got a compiling error saying 'duplicate interface definition for class `AppDelegate. 这样,这些文件就冲突了,AppDelegate导入了这些文件,并且出现了编译错误,提示“类'AppDelegate的接口定义重复”。

I resolved the problem by removing the reference and copying the files as intended. 我通过删除引用并按预期复制文件来解决了该问题。 This might not be your problem since you had a run time error, but just a heads up. 这可能不是您的问题,因为您遇到了运行时错误,但只有注意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM