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 1:
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.
If Xcode offers “New Group” and “New Group with Folder”, then “New Group” creates a group without a folder.
If Xcode offers “New Group” and “New Group without Folder”, then “New Group” creates a group with a folder.
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:
- Xcode figures out which group you clicked on (or, if you didn't click a group, it figures out which group contains the item you clicked).
- Xcode looks at the subgroups (and linked folders) that are children of the clicked group.
- If the majority of the subgroups have their own folders, then Xcode assumes you want the new group to also have its own folder. So Xcode shows “New Group” (which will create a folder) and “New Group without Folder”.
- If the majority of the subgroups do not have their own folders, then Xcode assumes you want the new group to also not have its own folder. So Xcode shows “New Group” (which will not create a folder) and “New Group with Folder”.
- I don't know how Xcode breaks the tie if exactly half of the subgroups have their own folders, and I don't feel like testing it right now…
- If the clicked group has no subgroups, Xcode decides what to show based on whether the clicked group itself has its own folder.
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.
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.
Solution 2:
For those who are too lazy to synchronize the folders, there is a nice tool on GitHub.
Synx is a command-line tool that reorganizes your Xcode project folder to match your Xcode groups.
Hope this helps somebody
Solution 3:
Have a look at andyvn22's answer here
To paraphrase:
- Xcode 3: Whenever you create a group in Xcode, immediately get info on it, and notice the "Path:" setting just below its name. Click "Choose..." and set it to a new folder with the same name.
- Xcode 4: Whenever you create a group in Xcode, immediately select it and open the righthand sidebar. Notice the "Path:" setting just below its name. Click the icon next to "None" (what is that, a file in a window?) and set it to a new folder with the same name.
Then you'll need to move any existing files on disk to the new folder manually.
Just tried this in my Xcode4 project, works like a charm.