views:

45

answers:

2

Hey internets!

So here's the deal: I have an image in the Resources folder of custom framework that I use as a default for classes in the framework. However, when I create these classes in projects that link against my framework they fail saying they can't find the image. I'm guessing the NSBundle's +mainBundle does not search the correct paths for my classes to find this image when linked in a different project. Is this correct? If so, where should I be looking for my image? I hope this makes sense (it's awfully late) and thanks for the help!

+2  A: 

For an application, the +mainBundle will be the application bundle (e.g., Foo.app). Frameworks are their own bundle, which you can find using +bundleForClass: or +bundleWithIdentifier:.

Jason Foreman
So, with the use of +bundleForClass:, I can call something similar to`NSImage* defaultImage = [[NSBundle bundleForClass:[MyClass class]] pathForResource:@"default" ofType:@"png" inDirectory:nil];`to grab the image I want? Is there a way I can do this a little more cleanly (I don't like using the `[MyClass class]` call)?
Grimless
The alternative is +bundleWithIdentifier, which you can call using the identifier you define in the bundle's Info.plist. What's wrong with `[MyClass class]`? It's a fairly common usage pattern and shouldn't make you uncomfortable.
Jason Foreman
It's not that it makes me uncomfortable, I was simply wondering if there was an alternative or more commonly used pattern. Which would you recommend? My gut tells me to use the +bundleWithIdentifier: since that can easily be #def'd and is not reliant on the actual content (classes or otherwise) of the framework. Thanks for the input!
Grimless
I personally use +bundleForClass: often, but you can use +bundleWithIdentifier: if that seems cleaner to you. The documentation claims that the latter may be a bit more efficient, so I'd say go with +bundleWithIdentifier:.
Jason Foreman
A: 

It's likely your image isn't getting put in the main bundle. You may have to use NSBundle directly to construct the path to your image. What does the built .app file hierarchy look like in terms of the image?

jtbandes
I don't believe the image is not in the framework bundle. My problem is that the class that is loading the image is using [NSBundle mainBundle] to grab the image. Since this call is being made by a dependent project, the "main bundle" is the application bundle and not the framework bundle, and therefor does not search the correct bundle to find the image. I need to find out what the best way is to always search the framework bundle to find that default image.
Grimless