如何在Xcode项目中包含txt文件? [英] How can I include a txt file in an Xcode project?

查看:514
本文介绍了如何在Xcode项目中包含txt文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加文件"IntegerArray.txt"的方法如下,首先进入文件"->将文件添加到"项目名称",选择文件,然后将其复制到项目的根文件夹中,即包含.xcodeproj文件.然后,我在Xcode中单击了项目的.xcodeproj文件,然后转到构建阶段"->复制文件"->"IntegerArray.txt",但是,当我构建项目时,以下代码不起作用

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

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

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

I expect to see the path to the file as output, but instead I see 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?

如果我有普通的Cocoa应用程序,那么当我添加文件时,它们会自动复制到内置程序的捆绑软件中.但是,使用Command Line Tool似乎并非如此,我开始怀疑Xcode的错误.

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.

推荐答案

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

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

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

Then from the File Menu select Add Files:

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

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

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

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.

然后,我要做的就是使用Finder将文件复制到项目的资源目录中.

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

在文件位于Resources目录(在Finder中)之后,回到Xcode中,选择项目中的Resources目录:

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

,然后再次使用文件"->添加文件"菜单选择.您应该看到文件突出显示(在这种情况下为affineOutput.txt):

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

双击它,它将成为捆绑软件的一部分.

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

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

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.

这篇关于如何在Xcode项目中包含txt文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆