user8472
user8472

Reputation: 3326

Classes and headers for multiple targets in Xcode

I have an @interface for class Foo defined in the header Foo.h and the corresponding implementation in the file Foo.m. If I add another implementation Foo.m of the class in a different directory to the Xcode project, I can specify the "Target Membership" in the "File" tab of the "Utilities" bar to assign the implementation to a specific target.

However, this does not work for header files. If I add a different header Foo.h for class Foo then I can not assign a "Membership" in the "File" tab of the "Utilities" bar. I always get an error "Duplicate interface for class 'Foo'", and then several errors "Property has a previous declaration" for each property I declare.

How can I use a class defined in different headers with the same name for different targets using Xcode 4.5.2?

UPDATE: I had already tried the solution suggested in this thread and it doesn't work in Xcode 4.5.2, the error message is as posted above.

Upvotes: 1

Views: 3258

Answers (1)

rmaddy
rmaddy

Reputation: 318944

Since your Foo class is only partially different between the two targets then I would suggest that you have one Foo.h and one Foo.m. You then use compiler directives to deal with the differences.

Go to the build settings for each target and add a flag under "Other C Flags". For example, for target A you can add:

-DTARGETA

and for target B you can add:

-DTARGETB

Then in Foo.h you can do:

@interface Foo : NSObject

@property commonProperty;
#if defined(TARGETA)
@property targetAProperty;
#elif defined(TARGETB)
@property targetBProperty;
#endif

@end

Do something similar for Foo.m.

Upvotes: 3

Related Questions