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. While compiling the .m file the corresponding .h file is included (and probably some other .h files are included). In any of these .h files AppDelegate.h is included. Plus you include it in the .m file. 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. 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. Use @class
statemenst where ever appropriate. 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. 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 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. 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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.