objective-cxcodexcode4file-organization

Workflow to create Xcode groups as file system folders


I like organizing my classes into groups, in the Xcode 4 project navigator pane. I also use GIT versioning via the terminal, which works better for me than the Xcode 4 integration. To me it makes the most sense when the groups I create in Xcode correspond to real folders on my file system.

However, Xcode seems to not care about where a file (e.g. Objective-C .h/.m file) is on the filesystem, and every time you add a group (Option+Command+N) it's purely cosmetic, for any files you put in there will share the same location on your hard drive as the rest.

The weird thing is, that if you first create a folder on your hard drive, and then drag it to Xcode, you have created a group with its own location on your file system. Every file you create in there will be created in the folder on disk. I love that, but I'd love it even more if Xcode would do that for me at Option+Command+N. The workflow of switching to Finder for every group you want to create and then dragging it back to the right place in your project navigator is just plain silly.

It feels like something a setting would fix, but I can't find it! Is there a better way?

Update: When dragging a folder to Xcode, you'll get a dialog window where you can choose "Folder Reference". That looks promising, however my Xcode 4 doesn't accept the files in there as classes and keeps telling me it can't find any files. Maybe the answer lies here somewhere, but I still can't find it.

Another update: I've changed my main editor environment from Xcode to AppCode. The latter supports actually creating folders to back the project groups, while maintaining 100% compatibility with Xcode. Those AppCode guys sure know what they're doing.


Solution

  • UPDATE

    The following describes Xcode 9 and later.

    Beginning with Xcode 9, Xcode can automatically create a filesystem folder when you create a group in your project.

    When you right-click (or control-click) in the project navigator, the context menu will include a “New Group” menu item and either a “New Group with Folder” menu item or a “New Group without Folder” menu item.

    In other words, “New Group” always does the opposite of the other (explicit) menu item, with regard to folder creation.

    If you rename a group, and that group has a corresponding folder with the same name, Xcode 9 also renames the folder.

    In Xcode 9, if you drag a file from one group to another, and the groups have different folders, Xcode 9 moves the file to the new group's folder, regardless of whether the new group's folder has the same name as the group.

    If you do not want this behavior in Xcode 9, you can set an undocumented setting from the command line:

    defaults write com.apple.dt.Xcode IDEDisableStructureEditingCoordinator -bool YES
    

    Impossible-to-deduce details about “New Group with Folder” and “New Group without Folder”

    It's not at all obvious how Xcode decides whether to show “New Group” and “New Group with Folder”, or “New Group” and “New Group without Folder”. An Apple engineer at WWDC 2018 (I think his name was Paul) looked at the Xcode source code and explained it to me:

    Basically, Xcode tries to guess whether your new group should have its own folder based on whether its future siblings have their own folders.

    ORIGINAL

    You can create a new group, with its own folder, directly in Xcode, but it's not at all obvious.

    Instead of choosing ‘New Group’, choose ‘Add Files to “<Project>”’. Then, in the file picker dialog, click the New Folder button or press Command-Shift-N and type the name of the new folder/group. Then, make sure “Create groups for any added folders” is chosen and click Add or press Return.

    adding a group with its own folder

    This doesn't save you much if you need to move existing files into the new group, because Xcode still won't do that.

    But it does save a few steps if you are creating a new group to hold new files.