我得到了运行时错误,因为类app delegate.So的重复接口定义,这段代码有什么问题。
#import <UIKit/UIKit.h>
@class ViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;
@end发布于 2013-02-08 18:20:37
在头文件状态的开头:
#if !defined APPDELEGATE_H
#define APPDELEGATE_H在结束状态下:
#endif这个错误的根本原因很可能是你在一些类头文件和.m文件中包含了AppDelegate.h。在编译.m文件时,会包含相应的.h文件(可能还会包含其他一些.h文件)。这些.h文件中的任何一个都包含AppDelegate.h。另外,您可以将其包含在.m文件中。从编译器的角度来看,这将导致接口的重复定义。上面的解决方案并不是真正的解决方案。严格地说,这是一种变通方法。但它是非常标准的,苹果在所有的模板中都使用了它。这只是一种变通方法,因为它并没有解决问题,而是解决了问题。
正确的解决方案是:如果可以避免,请不要在.h文件中包含其他.h文件。在适当的地方使用@class statemenst。如果.h文件已经包含在任何其他包含的.m文件中,请不要在该.h文件中重复包含该.h文件。你可能会想“这是一个痛苦的……”。您是对的:)因此,我建议使用通用的#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发布于 2013-02-08 19:30:13
我刚刚遇到了这个问题。
我所做的是从另一个项目中拖放具有#import AppDelegate的文件,该项目也包含准确命名的AppDelegate.h/.m类。当我将文件放到我的项目中时,我从该项目中引用了它们,而不是复制它们。
通过这样做,这些文件与要导入的AppDelegate相冲突,并且我得到了一个编译错误,显示'duplicate interface definition for class `AppDelegate‘。
我通过删除引用并按预期复制文件解决了这个问题。这可能不是你的问题,因为你有一个运行时错误,但只是一个提醒。
https://stackoverflow.com/questions/14770006
复制相似问题