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

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)
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?
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.

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.
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.
Then the way I do it is to use Finder to copy the file into the Resources Directory of my project.
After the file is in the Resources Directory (in Finder), back in Xcode select the Resources directory in your project:
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.
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.

If you include in the Resources Folder it should be included.
You may need to select the Project and Target.
In Build Phases check is there is entry under Copy Bundle Resources

Related

Xcode add reference to files instead of copy

I have created my framework that i want to add in my project. What i do is:
In my project i chose Xcode -> File -> Add Files to..
In prompt window i go for framework directory, chose .xcodeproj file
Check "Copy items if needed" and "Added folders - Create groups" radio button
What i expect - .xcodeproj that contain my framework is added and copied to my project
What i got - i can see file in Xcode, but not in folder when my app exist. So, when i try to Delete -> Move to trash my framework, it delete it in folder when it was created (not my project folder).
I can't figure out how to actually add and copy file instead of reference to it.

How do I access a resource file in Xcode?

To access the resource file namefile from a program:
I ostensibly just need to check the box "target membership" to identify it as a resource file:
but this box is disabled.
What am I missing?
The helloworld target in your project is configured as a command-line tool (the square black icon that looks like a Terminal indicates this). Those compile to a single, standalone file thus Xcode cannot embed a resource file with it (which is why it's disabled).
You need to build a "Cocoa Application" target if you want to be able to include resource files. You can start a new project using the Cocoa Application template or manually add a target to your current project. You'll probably find it easier to start with a new project.
Add resource files to a command line tool
Add the file to the "Copy Files" section of your project's Build Phases:
Make sure to set Destination to "Resources", clear Subpath, and untick Copy only when installing.
Then whenever you build, Xcode will copy the file into your app's build directory, right next to the executable:
That's a screenshot of the ~/Library/Developer/Xcode/DerivedData/[your project]/Build/Products/Debug folder.
This method also works when you archive your app.
Note: if you want the file to be in some subfolder relative to the executable, e.g. res/images/, set the Subpath to that path.

Suggestions Please: File in Xcode Project but Cannot be Found

I have a file that shows up in "Open Quickly" on Xcode and shows up no place in the project if I do "Reveal in Project Navigator". It does compile correctly into several targets.
If I add the file to the project, the project cannot compile because the file is already included. How can I find this inclusion?
Note: The file does NOT show up in "Open Quickly" on App Code, but I can navigate to it via Command-B.
In AppCode you can use Navigate | File... (Cmd+Shift+O) and check Include non-project files - the file should be shown in the list.
Also, you can open the file in editor, switch navigator to Files mode and use Navigate | Select in... (Alt+F1) | Project View - the file will be shown in its actual location.
After that use Manager Targets action from context menu to see what targets this file belong to.
This bizarre situation happens because you are importing a .m file in one of your #import statements. If you fix this, the project will no longer compile, because the file will not be automatically included.

Add source files to cocoa project not intended for compile

I'm writing a cocoa application which should copy files to a given directory. The files should be part of the app, but not compiled. I'm thinking of a folder, e.g. "MySources", where I want to add all .a, .m and .xip files, which should not be compiled. These files should be left as they are - so plain and editable text. Then my app provides a button and when a user clicks on that button, the app copies all files within the "MySources" folder to a given directory.
Add a Copy Files build phase, configure it to copy to the desired destination within the app bundle, and add the desired files to that build phase.
In Target > Build Phases remove the appropriate files in Compile Resources and add Copy Files build phases to copy the files to the destination locations in the bundle.

Where are the app's build folders located?

Where are the app's build folders located? I cannot see anything in the app directory.
Build folder is located in Derived Data folder. You can open this folder through Organizer on Project tab.
In Xcode, on the left hand side where it lists your files, right click on your app (e.g. 'MyApplication.app' and click on 'Show in Finder'.
Hope this helps.
After you create the build in XCode, press Command+7 to view the Log. The log will display the exact path where the final .app file will be located.
Something like the string below;
Touch /Users/macmini15/Library/Developer/Xcode/DerivedData/test-ciynarecplxqjqdpmuonamtecgvn/Build/Products/Debug-iphoneos/test.app
cd /Users/macmini15/Desktop/test
setenv PATH "/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/usr/bin/touch -c /Users/macmini15/Library/Developer/Xcode/DerivedData/test-ciynarecplxqjqdpmuonamtecgvn/Build/Products/Debug-iphoneos/test.app

Resources