Duplicate interface definition for class 'Resources.h'

Viewed 430

Due to security reason cannot paste the original code hence tried to be more intent full

  1. Have a project called Utils.xcodeproj which has an objective c files called Resources.m and its header is exposed as public header.
  2. Have another project called main.xcodeproj in which this Utils project is referenced.

Now when compiling the main.xcodeproj I get error "Duplicate interface definition for class Resources" iOS/Utils/Utils/Resources.h:23:1: @interface Resources : NSObject

Build/Products/Debug-iphoneos/include/Utils/Resources.h:23:12: note: previous definition is here @interface Resources : NSObject

Is there a way to overcome this error or turn this error into a warning?

Class Resources.h

@protocol ResourcesDelegate;

@interface Resources : NSObject

@property (weak, nonatomic) id<ResourcesDelegate> delegate;
+ (void) setDefault:(UIView *)view;
@end


@protocol ResourcesDelegate <NSObject>
@required
- (void)loadFromSDK:(NSString*)Name;

@end



Class Utils-Bridging-Header.h
#import "Resources.h"

Class XYZ in main.xcodeproj
#import "Utils/Resources.h"

[Resources setDefault:view]
1 Answers

also you could..

#ifndef Resources_h
#define Resources_h

// ... your #import or @import, #include <...> rules here...

// ... announcing a 
// @class SomeClassWithName
// here if needed. This will make SomeClassWithName available
// to this header even if its interface is declared later on. (skaak's solution)

// ... all your @interface stuff here ...

#endif

This should keep it from being implemented twice per project.

Why is this working?
Because once #import "Resources.h" is done Resources_h would be defined and when #import "Utils/Resources.h" is processed the definition is jumping over the second attempt to declare the class.

Edit forgot to mention that this approach should be used in header files of course. While it is not forbidden to make use of it in implementation files. In fact in cross platform source you will see this is used heavily.

Edit2 have a look at the following nice explained answer what is going on when #include is used and what was meant to work fine when using #import sven's stackoverflow answer

  • so the #import preprocessor command reminds the files that have been imported already, but that seems to fail when files and with it declarations are re-established from another project with its own folder structure. In short: "Resources.h" and "Utils/Resources.h" are different files to the preprocessor so your class definitions are duplicate of each other.
Related