如何在 Xcode 项目中包含 txt 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27642521/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:24:53  来源:igfitidea点击:

How can I include a txt file in an Xcode project?

xcodemacosswiftprojectnsbundle

提问by 11Kilobytes

I added a file "IntegerArray.txt" as follows, first I went to File -> Add file to "project name", I selected my file and it was copied into the root folder of my project, the same one that contains the .xcodeproj file. Then I clicked on my project's .xcodeproj file in Xcode, after which I went to Build Phases -> Copy Files -> "IntegerArray.txt", however when I build my project, the following code doesn't work

我添加了一个文件“IntegerArray.txt”,如下所示,首先我去文件 - >将文件添加到“项目名称”,我选择了我的文件并将它复制到我的项目的根文件夹中,同一个包含 . xcodeproj 文件。然后我在 Xcode 中点击了我的项目的 .xcodeproj 文件,之后我去了 Build Phases -> Copy Files -> "IntegerArray.txt",但是当我构建我的项目时,下面的代码不起作用

let bundle = NSBundle.mainBundle()
let path = bundle.pathForResource("IntegerArray", ofType: "txt")
println(path)

I expect to see the path to the file as output, but instead I see nil.

我希望看到文件的路径作为输出,但我看到的是 nil。

So I want to copy a file to my project in such a way that the above code works. How can I do this?

所以我想以上述代码工作的方式将文件复制到我的项目中。我怎样才能做到这一点?

If I have a normal Cocoa Application, then when I add files they are automatically copied to the built program's bundle. However, with Command Line Tool's this doesn't seem to be the case, I'm starting to suspect a bug with Xcode.

如果我有一个普通的 Cocoa 应用程序,那么当我添加文件时,它们会自动复制到构建的程序包中。但是,对于命令行工具,情况似乎并非如此,我开始怀疑 Xcode 存在错误。

回答by jwlaughton

If you don't have a Resources directory in your project, select your project:

如果您的项目中没有 Resources 目录,请选择您的项目:

enter image description here

在此处输入图片说明

Then from the File Menu select Add Files:

然后从文件菜单中选择添加文件:

enter image description here

在此处输入图片说明

In the window that opens click on the "New Folder" button in the lower left hand corner of the window.

在打开的窗口中,单击窗口左下角的“新建文件夹”按钮。

A small pop-up window will open; type Resources in that window for the folder name then click the "Create" button. You should now have a Resources folder in your project.

将打开一个小弹出窗口;在该窗口中为文件夹名称键入资源,然后单击“创建”按钮。您现在应该在您的项目中有一个 Resources 文件夹。

Then the way I do it is to use Finder to copy the file into the Resources Directory of my project.

然后我的做法是使用 Finder 将文件复制到我的项目的资源目录中。

After the file is in the Resources Directory (in Finder), back in Xcode select the Resources directory in your project:

文件位于资源目​​录(在 Finder 中)后,返回 Xcode,选择项目中的资源目录:

enter image description here

在此处输入图片说明

and use the File-> Add Files menu selection again. You should see your file highlighted (affineOutput.txt in this case):

并再次使用 File-> Add Files 菜单选项。您应该会看到您的文件突出显示(在本例中为 affineOutput.txt):

enter image description here

在此处输入图片说明

Double Click on it and it will become part of the bundle.

双击它,它将成为捆绑包的一部分。

As Milliways says, then check the Build Phases in your project to be sure the file is in the Copy Bundle Resources. If it's somewhere else, just drag it into the Copy Bundle Resources.

正如 Milliways 所说,然后检查项目中的构建阶段以确保文件位于复制捆绑资源中。如果它在其他地方,只需将其拖到复制捆绑资源中即可。

回答by Milliways

If you include in the ResourcesFolder it should be included.

如果您包含在Resources文件夹中,则应将其包含在内。

You may need to select the Projectand Target. In Build Phasescheck is there is entry under Copy Bundle Resources

您可能需要选择ProjectTarget。在Build Phases检查下是否有条目Copy Bundle Resources