如何排除 Xcode 项目包中的图像文件,有条件地用于发布版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5214698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to exclude image files in Xcode project bundle, conditionally for Release version?
提问by petershine
I added .png images to the Xcode project for conditional use like making screenshots of a view
However, since this is not needed for the Release version of the app, I would like to find a way to exclude them using some kind of settings for Target. I expect there can be a solution like using #if DEBUG
macro for Debug compilation, which can work for lines of source code. But, in case of files included in project bundle, I am having trouble finding the answers.
我将 .png 图像添加到 Xcode 项目中,以便有条件地使用,例如制作视图的屏幕截图 但是,由于应用程序的 Release 版本不需要这样做,因此我想找到一种使用 Target 的某种设置来排除它们的方法. 我希望可以有一个解决方案,比如使用#if DEBUG
宏进行调试编译,它可以用于源代码行。但是,对于项目 bundle 中包含的文件,我很难找到答案。
回答by Caleb
In Xcode 3, there's a view above the editor that lists the files in the project. There's a checkbox on the right side of that view for each file, and you can uncheck the box to remove the file from the current target.
在 Xcode 3 中,编辑器上方有一个视图,列出了项目中的文件。每个文件的视图右侧都有一个复选框,您可以取消选中该框以从当前目标中删除该文件。
In Xcode 4, show the Project Navigator on the left side of the window, and show the File Inspector on the right side, in the Utilities area. When you select a file, you'll see a Target Membership area with a list of targets and checkboxes. If you want to exclude the file from a particular target, uncheck the box next to that target. Here's a picture:
在 Xcode 4 中,在窗口左侧显示 Project Navigator,在右侧的 Utilities 区域中显示 File Inspector。当您选择一个文件时,您将看到一个包含目标和复选框列表的目标成员资格区域。如果要从特定目标中排除文件,请取消选中该目标旁边的框。这是一张图片:
This is a bit different from excluding files from only some builds of a single target. Still, I think it's the simplest mechanism to use for the situation that you describe. Simply duplicate your existing target so that you have a copy that you can use for making screenshots. Remove the extra files from your production target but leave them in the screenshot target, as described above.
这与仅从单个目标的某些构建中排除文件有点不同。尽管如此,我认为这是用于您描述的情况的最简单的机制。只需复制您现有的目标,即可拥有一份可用于制作屏幕截图的副本。从生产目标中删除多余的文件,但将它们保留在屏幕截图目标中,如上所述。
回答by my fat llama
A target's inputs are the same for all builds, so there's no checkbox that will do it for you.
目标的输入对于所有构建都是相同的,因此没有复选框可以为您执行此操作。
All that really happens though is that image files like .png or whatever get added to the copy bundle resources phase. You can remove them from that phase and instead create a custom script build phase using a shell script.
然而,真正发生的是像 .png 或任何被添加到复制包资源阶段的图像文件。您可以从该阶段删除它们,而是使用 shell 脚本创建自定义脚本构建阶段。
It will default to printing out all the environment variables set by xcode, from there you should be able to write a script which only performs the copy when say ${BUILD_STYLE} is 'Debug'.
它将默认打印出 xcode 设置的所有环境变量,从那里您应该能够编写一个脚本,该脚本仅在 ${BUILD_STYLE} 为“调试”时执行复制。
You probably want ${BUILD_STYLE}, ${CONTENTS_FOLDER_PATH} and ${INPUT_FILE_PATH} for starters.
您可能需要 ${BUILD_STYLE}、${CONTENTS_FOLDER_PATH} 和 ${INPUT_FILE_PATH} 作为初学者。