64

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.

epologee
  • 10,851
  • 9
  • 64
  • 102

3 Answers3

132

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.

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.

rob mayoff
  • 342,380
  • 53
  • 730
  • 766
  • 1
    I do that in every other app and it never occurred to me to do it in Xcode, clever! Personally, I do 95% of my coding in AppCode nowadays, which auto-creates folders when adding groups. Thanks for the contribution! – epologee Jun 25 '13 at 09:55
  • P.S. What app do you use to create those animated GIFs? – epologee Apr 29 '14 at 18:52
  • 5
    I used ScreenFlow to record the screen and PhotoShop to turn the movie into a GIF. However, if I were doing it now, I'd check out [LICEcap](http://www.cockos.com/licecap/) first. It's free and it records the screen directly to GIF. – rob mayoff Apr 29 '14 at 19:18
  • Thank you! I didn't know that I can do such a useful thing. Nice! :) – Godlike Feb 05 '16 at 08:30
  • **There is not use of creating physical folder**, as even you can not move existing files and create new swift file on this folder with Xcode 7.3, only you can drag and add new files, I feel there is no use of doing such efforts – swiftBoy Apr 05 '16 at 14:38
  • Yes, I mentioned in my answer that “This doesn't save you much if you need to move existing files into the new group, because Xcode still won't do that.” If you need to move existing files, you must use the Finder or the command line, then re-locate the files in Xcode. – rob mayoff Apr 05 '16 at 15:28
  • The 'New Group without Folder' command appears to just be called 'New Group' again, and there's now a 'New Group with Folder' command. (Xcode 9.4) – Kal Jun 06 '18 at 03:53
  • I can get both “New Group with Folder” and “New Group without Folder” to show up, depending on where I right-click in my Project Navigator. I haven't yet figured out exactly how it decides which to offer. – rob mayoff Jun 06 '18 at 07:43
  • 1
    @Kal I got clarification about the New Group menu items from an Apple engineer and have added it to my answer. – rob mayoff Jun 07 '18 at 02:33
  • Nice detective work Rob. Not sure what I think about Apple's logic here. It's quite odd to have a command swap behaviour like that depending on context. Well, I suppose it's only as consistent as the user chooses to be! – Kal Jun 08 '18 at 07:47
39

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.

enter image description here

Hope this helps somebody

E-Riddie
  • 14,052
  • 7
  • 47
  • 70
  • 18
    I love this but it's a shame that it has to exist. – Pat Niemeyer Dec 16 '15 at 01:05
  • 1
    @EridB How can this be done in Xcode 9? Maybe create a new answer with your mentioned possibility. – Flupp Sep 19 '17 at 08:43
  • i love this, but its shame that it has to exist ;) – LeXeR Jun 14 '18 at 10:22
  • This is a great little tool... however a warning if you are using git it does not move files using git, it just uses the filesystem which will clobber your edit history in git :( So, it's a trade off... things can be fixed by hand in Xcode by tediously making new folders/moving files by hand. You can still revert and look at old history but it creates a break between the old/new setup. Perhaps git has a command for finding misplaced files? In my case I didn't mind losing the old history so it was fantastic for sorting out my 200 files and dozens and dozens of nested groups – Jc Nolan Apr 15 '21 at 23:47
5

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.

Community
  • 1
  • 1
  • 1
    I've switched to [AppCode](http://www.jetbrains.com/objc) for my main development tasks, and it manages the folders and groups for me. But thanks for the addition. – epologee Jun 28 '12 at 08:48