Search code examples
iosobjective-cnsbundle

'NSBundle mainBundle' pathForResource strange behavior


I can't figure out any rhyme or reason on how pathForResource works... or more appropriately, doesn't.

I have a bunch of nibs in my app, they are all properly in /Build Phases/Copy Bundle Resources.

They also are properly ticked in Target Membership on the File Inspector tab.

I'm only showing 2 cases here for brevity. When I get to the part of my code where I use pathForResource I get arbitrary and inconsistent results. Even though ALL of my nibs are named ____.xib, in some cases, the only way I get a result back from pathForResource is if I ask for xml and other times I have to ask for nib

Anyone have an idea why this behavior is inconsistent?

enter image description here


Solution

  • These results don't seem especially surprising or inconsistent to me.

    .xib files are source files for NIBs. They should not get copied directly into an app. They are compiled at build time to .nib files and it is those .nib files which get copied into the app. You should not expect pathForResource:... ofType:@"xib" to ever find a .xib file in your app. If it does, you've somehow screwed up the building of the app. The .xib file is not useful at run time.

    Presumably unrelated to that, you have a file TabSale.xml that's in the Copy Bundle Resources build phase. That file is just copied directly. If you didn't intend to copy a raw XML file — if, for example, that's a source file that's supposed to be converted to something else at build time — then you need to figure out why it's being copied. If it's just a bogus copy of TabSale.xib for some reason, you should just get rid of it.