How to include txt file in Xcode project?

I added the “IntegerArray.txt” file as follows: first I went to the file → Add file to “project name”, I selected my file and was copied to the root folder of my project, the same as the .xcodeproj file. Then I clicked on the .xcodeproj project file in Xcode, after which I went to Build Phases → Copy Files → "IntegerArray.txt", however, when creating my project, the following code does not work.

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

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

So, I want to copy the file into my project so that this 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-in software package. However, using the Command Line Tool, it doesn't seem like I'm starting to suspect an error with Xcode.

+1
source share
2 answers

If you do not have a resource directory in your project, select your project:

enter image description here

Then, on the File menu, select Add Files:

enter image description here

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

A small popup window will open; enter resources for the folder name in this window, then click the "Create" button. You should now have the Resources folder in your project.

Then the way I do this is to use Finder to copy the file to the resource directory of my project.

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

enter image description here

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

enter image description here

Double click on it and it will become part of the package.

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

+4
source

If you have included in the Resources folder, it should be included.

You may need to select Project and Target . In Build Phases check if there is an entry in Copy Bundle Resources

0
source

Source: https://habr.com/ru/post/1013297/


All Articles