6

I have a static library that gets linked to by an application. The library code opens a file that is in bundle that is in the application bundle, the opening is done as:

NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"Config" ofType:@"plist"];

This is working fine.

However I want to add some unit test code to the library, and so I have a logic test target for it. As the file is in the bundle for the application and not in the bundle for the static library I copied the Config.plist file and added it to the test code target via Copy Bundle Resources. However when I execute the test code the file cannot be found. Why is that?

In that the above is confusing, here's a summary of the workspace structure.

Workspace contains:
    Application Project with application target, which contains (X)
        Config.plist (a)
    Library project which contains:
        Library target, which contains:
            the code opening the file in the bundle (b)
        Test library target, which contains: (Y)
            A Copy of the Config.plist (c)

So if I build X then when b runs it can find a. But when I build Y when it runs then b can't find c.

Gabriel.Massana
  • 8,165
  • 6
  • 62
  • 81
Gruntcakes
  • 37,738
  • 44
  • 184
  • 378

3 Answers3

11

I found if I changed [Bundle mainBundle] to [NSBundle bundleForClass:[self class]] then it works in both cases

Gruntcakes
  • 37,738
  • 44
  • 184
  • 378
0

You can create an application target in project Library, copy that file to that target and then create an application test target(within the same project) where you test your library code.

e1985
  • 6,239
  • 1
  • 24
  • 39
0

The problem is a static library doesn't have bundle resources. Once compiled it will only consist in compiled code and header files. Therefore there is no static library plist resource to copy.

So you need another process to copy the library bundle such that it ends up in the build directory.

The most effective way I have found is to do this with a script in a run script phase before the Link Binary With Libraries phase. I attach a screenshot of one of my projects where I have had to do something similar. You should be able to achieve what you need by adapting the file name on the first row of the script. All the environment variables are pre-defined as standard, so I don't think there is any further configuration required other than this script.

Haven't tested if the environment variables work with anything other than the default xCode 4 build locations though.

Script to copy resource from static library to Parent Project build directory

TheBasicMind
  • 3,585
  • 1
  • 18
  • 20
  • A static library doesn't have a bundle, however a logic test target for a static library does. – Gruntcakes May 01 '13 at 17:00
  • However a library project does. In your configuration above you state you have the library project. Looking again at your configuration, I see what I have provided doesn't apply to how you have it set-up. Having said that, if your library test target requires the config plist, you would be best off storing one copy of the data your library needs in a config.plist in the library project. Then you can use the technique above to copy the plist data from the Library into your main project. The overall result should then be a more modular build process. – TheBasicMind May 02 '13 at 15:37
  • I said I have a logic test library, and in the structure there is a library project and a test library project both shown, and the plist is shown as being part of the test library. Not trying to argue - I appreciate your reply. Thanks. – Gruntcakes May 02 '13 at 17:58