Mustafa
Mustafa

Reputation: 20564

Settingup Xcode project to add new file in custom directory

How can i setup my Xcode project so that it creates new class files (.h/.m) in Classes directory and new interface files (.xib/.nib) in Interfaces directory?

By default Xcode adds new files in the root project directory, and i have to manually put these into Classes and Interfaces directories.

Edit:

I'm referring to the Xcode File > New File... option. I want the new files created from the Xcode project to move automatically to corresponding directories e.g. .h/.m files get automatically created in Classes directory, and .xib get automatically created in Interfaces directory etc. And i mean physical directories, not Xcode "Groups".

Upvotes: 4

Views: 3168

Answers (2)

zneak
zneak

Reputation: 138031

I assume you've made a group (folder) called Classes, or something along these lines, inside your Xcode project. Select it and do Command+I (or right-click it and select Get Info). Change the "Path" option to the place where you want files to be created.

Please note that it will only affect files you create on it. Creating them elsewhere then moving them to this group will not move them into the directory.

EDIT The above answer applies to Xcode 3. In Xcode 5, you would need to select the logical group in the Project Navigator (first navigator tab, bring it up with ⌘1) and then inspect it with the File Inspector (first utility tab, bring it up with ⌥⌘1). The default physical folder for all the new files can be changed by clicking the folder icon below the Location dropdown menu.

Upvotes: 8

cbowns
cbowns

Reputation: 6365

In Xcode 5, it auto-selects the default folder on disk to match to the project folder in the sidebar for certain folders, and it is possible to manually establish this linkage.

In my project, I have sidebar groups for AppName, AppNameTests, and one I created myself called Shared Components. When I select the AppName group in the sidebar and create a class, the Save dialog automatically drills into the AppName subdirectory inside the top-level AppName folder on disk, and does the same for classes created inside AppNameTests.

I was unable to get Xcode to mimic this behaviour with my custom Shared Components sidebar group, despite having a Shared Components folder on disk, but I figured out how to coerce it!

Xcode simply needed to know that the on-disk folder was what my sidebar group was all about. To do that:

  1. Open your project's folder up in the Finder
  2. Drag the folder you'd like linked into the sidebar into the project organizer.
  3. When Xcode presents its "Add Files" dialog, select "Create groups for any added folders". (These are the dialog options I selected:

add files dialog

Now, when I select this sidebar group and create a file, the Save dialog starts in the folder I dragged into the project sidebar. Victory!

Edit: I discovered the keys that Xcode uses in the project.pbxproj file for this: if you change a folder's name key to path, i.e.

path = "Shared Components";

that's basically what Xcode is doing when you add a folder to the project as a group reference. (You can also use both name and path keys if you want the sidebar name to be different than the on-disk path.)

Upvotes: 0

Related Questions