简体   繁体   English

'NSBundle mainBundle'pathForResource奇怪的行为

[英]'NSBundle mainBundle' pathForResource strange behavior

I can't figure out any rhyme or reason on how pathForResource works... or more appropriately, doesn't. 对于pathForResource工作方式,我想不通任何押韵或理由……或更恰当地说,没有。

I have a bunch of nibs in my app, they are all properly in /Build Phases/Copy Bundle Resources. 我的应用程序中有一堆笔尖,它们都位于/ Build Phases / Copy Bundle Resources中。

They also are properly ticked in Target Membership on the File Inspector tab. 也可以在“ File Inspector选项卡上的“ Target Membership中正确选中它们。

I'm only showing 2 cases here for brevity. 为了简洁起见,这里仅显示2个案例。 When I get to the part of my code where I use pathForResource I get arbitrary and inconsistent results. 当我到达使用pathForResource代码部分时,会得到任意且不一致的结果。 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 即使我所有的笔尖都命名为____。xib,在某些情况下,我从pathForResource返回结果的唯一方法是我是否要求xml而其他时候我必须要求nib

Anyone have an idea why this behavior is inconsistent? 有人知道为什么这种行为不一致吗?

在此处输入图片说明

These results don't seem especially surprising or inconsistent to me. 这些结果对我来说似乎并不特别令人惊讶或矛盾。

.xib files are source files for NIBs. .xib文件是NIB的源文件。 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. 它们在构建时被编译为.nib文件,这些.nib文件被复制到应用程序中。 You should not expect pathForResource:... ofType:@"xib" to ever find a .xib file in your app. 您不应该期望pathForResource:... ofType:@"xib"在您的应用程序中找到pathForResource:... ofType:@"xib"文件。 If it does, you've somehow screwed up the building of the app. 如果确实如此,则说明您已经搞砸了应用程序的构建。 The .xib file is not useful at run time. .xib文件在运行时无用。

Presumably unrelated to that, you have a file TabSale.xml that's in the Copy Bundle Resources build phase. 大概与此无关,您有一个文件TabSale.xml处于复制捆绑资源构建阶段。 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. 如果您不打算复制原始XML文件(例如,如果该原始文件应该在构建时转换为其他文件),那么您需要弄清楚为什么要复制它。 If it's just a bogus copy of TabSale.xib for some reason, you should just get rid of it. 如果由于某种原因它只是TabSale.xib的虚假副本,则应该删除它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM