4

I've created a XCode projet that contains 2 targets and static library that I implement in both targets. Some of the classes of my static library needs to be slightly different depending on the target I compile.

I do not have any idea how to do it properly. Any idea?

Thanks

Niko
  • 2,543
  • 1
  • 24
  • 29
  • You can't do this at compile time with a static library, because it's just that - a static library. It doesn't care about what platform you are targeting in the product in which it's used. Period. If you tell us exactly what you need to do, we can probably come up with a runtime solution. – Richard J. Ross III Sep 12 '12 at 12:36
  • 1
    Separate static lib into another Xcode project, create two targets inside this new Xcode project and change behavior based on these two targets. And in your origin project set different target dependencies and linking. Look at SDWebImage (https://github.com/rs/SDWebImage) as an example - there're two targets SDWebImage and SDWebImage ARC and these targets produces two different static libraries (libSDWebImage.a and libSDWebImageARC.a) to link against. – zrzka Sep 12 '12 at 12:44
  • I like the idea of creating several targets in my static library :-). Thanks @RobertVojta – Niko Sep 12 '12 at 12:56
  • You could separate out the parts of the library that need to be different between the targets into a different library, and build two versions of the new library. – Dan Sep 12 '12 at 13:40

1 Answers1

0

In what way to they need to be different? (I am basing my answer on a guess that it can be handled by setting some sort of state-variable in the classes or by a custom init method)

I would suggest you create some form of custom init methods or instance variables that you set for the classes that need to have different behaviors. You can then use User defined setting in your build settings for each target.

Check this question & answer for more information: iphone: get User Defined variable in Target's setting by code?

Basicly you could have a setting that would be a string like so: "Standard", you fetch it from the

FooBarClass.h

typedef enum {   FooBarSettingNormal,   FooBarSettingFast } FooBarSetting;

-(id)initWithSetting:(FooBarSetting)setting;

And then fetch the variable set in buildsetting from the code and init the FooBar object like so:

SomeViewController.m

NSNumber* fooBarSetting = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"FooBarSetting"];
FooBar * baz = [[FooBar alloc] initWithSetting:[fooBarSetting intValue]];

This enables you to have different behavior in your classes and keeping the static library separate and stand alone from the project you are using.

I hope you find this somewhat helpful :)

Community
  • 1
  • 1
oehman
  • 249
  • 4
  • 12