4

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".

Mustafa
  • 20,504
  • 42
  • 146
  • 209

2 Answers2

8

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.

zneak
  • 134,922
  • 42
  • 253
  • 328
  • Thanks for posting the answer. Actually, i was talking about the 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". – Mustafa Aug 02 '10 at 07:36
  • @Mustafa: I don't know how you can do that (if you can). Usually, to create files, I right-click groups and do "Add -> New File...", which creates them in the folder the group points to. – zneak Aug 02 '10 at 14:48
  • I figured out a way to accomplish this, I've added an answer. – cbowns Jan 29 '14 at 21:19
  • I saw that. I commented on your answer too. – zneak Jan 29 '14 at 21:20
0

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

cbowns
  • 6,295
  • 5
  • 47
  • 64
  • Isn't it a little harsh to downvote 4-year-old answers because the UI evolved? :) Once you have a logical group created in Xcode 5, the correct way to assign it to a specific folder is to select it in the left bar, select the File Inspector in the right bar (the tab with the document icon) and click the folder icon below the Location dropdown. This lets you pick a physical folder where all the new files in this logical group will be created by default. – zneak Jan 29 '14 at 20:46
  • Oh interesting! I couldn't find the Location info for the life of me in the File Inspector pane, but it's because I had the Identity and Type subsection collapsed. Derp! – cbowns Jan 30 '14 at 18:09