Calaf
Calaf

Reputation: 10817

How do I access a resource file in Xcode?

To access the resource file namefile from a program:

main.cpp

I ostensibly just need to check the box "target membership" to identify it as a resource file:

target membership

but this box is disabled.

What am I missing?

Upvotes: 2

Views: 965

Answers (2)

McKinley
McKinley

Reputation: 1394

Add resource files to a command line tool


Add the file to the "Copy Files" section of your project's Build Phases:

Build Phases screen

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:

Xcode build folder

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.

Upvotes: 0

par
par

Reputation: 17724

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.

Upvotes: 1

Related Questions