44

I have an Xcode project with the following group structure:

ProjectName/
    Classes/
        class1.h
        class1.m
        class2.h
        class2.m
        ...
    XMLDocs/
        doc1.xml
        doc2.xml
        ...
    Resources/
        Info.plist
        MainMenu.xib
        ...

and so on and so forth.

I'd like this to be represented in the folder structure on the disk as well:

ProjectFolder/
    Project.xcodeproj
    Classes/
        class1.h
        class1.m
        class2.h
        class2.m
        ...
    XMLDocs/
        doc1.xml
        doc2.xml
        ...
    Resources/
        Info.plist
        MainMenu.xib
        ...

as opposed to the usual everything in the root project folder methodology.

Is there any way to do this without manually creating the folder structure on the disk and then having to repoint all the reference in Xcode to the new location?

Bonus points if you can tell me how to do this automatically any time I create a new group or add files to an existing group in Xcode.

Chris Hanson
  • 54,380
  • 8
  • 73
  • 102
Redwood
  • 66,744
  • 41
  • 126
  • 187
  • I've wanted to do this for ages now. Just yet another simple thing that Xcode can't seem to deal with. At least I now know the functionality doesn't exist for sure. Thanks for a great question! – John Gallagher Mar 24 '10 at 22:26
  • Actually, there is a way - https://github.com/venmo/synx – ULazdins Dec 26 '17 at 18:17
  • I recently found a Command Line Tool that organizes your folder structure. It's simple to setup and simple to use. For more details, check out [another answer of mine about the same folder structure issue](https://stackoverflow.com/questions/18661305/folder-structure-in-xcode-vs-folder-structure-in-finder/24101005#24101005). – Cornel Jun 09 '14 at 13:26

6 Answers6

63

You can do this to some extent! There's just one extra step to do every time you create a new group (and this first time you'll have a lot of catching up to do!):

Xcode location setting

Whenever you create a group in Xcode, immediately select it and open the righthand sidebar. Notice the "Location:" setting just below its name. Click the folder icon next to "None" and set it to a new folder with the same name.

Now, whenever you add files to the group, they'll head to the corresponding folder on disk. Woo!

Unfortunately, this initial time, you'll have a lot of file-moving to do. (As you change the group paths, your files will all turn red, as Xcode will begin looking for them in the new folder. You'll have to manually move them from the Finder to fix it.) There is, however, a command-line tool to perform this initial migration for you: https://github.com/venmo/synx

andyvn22
  • 14,696
  • 1
  • 52
  • 74
11

A folder reference is what you want. When dragging or adding files to the project there is an option to "Create Folder References for any added folders". Choose that and you'll get the behavior you want.

alt text http://img.skitch.com/20081203-prtxsp7c36ern4afxxdixy93sq.png

You could try adding the entire enclosing folder of the project as a folder reference. That should get everything. Or add references for "Classes" "Resources" and "XML" individually.

amrox
  • 6,207
  • 3
  • 36
  • 57
  • Thanks, that helps for adding future items, but is there any way to turn the existing groups into folders? – Redwood Dec 04 '08 at 01:26
  • You only get this option when dragging in files you don't get this option when adding new files – TheLearner Oct 07 '11 at 11:08
  • This is a great solution for adding resources. You may have noticed the blue folder 'www' in PhoneGap. Just add anything to that folder and it's added to your project. However if you go to the "Copy Bundle Resources" in the Build Phases section of your project, you will notice that only the folder appears there but not its contents. To get to the contents of your referenced folder you need to append the path to the resource you want with the name of your added folder, like so: NSURL *htmlURL = [[NSBundle mainBundle] URLForResource:@"mypage" withExtension:@"html" subdirectory:@"www"]; – aeldron Dec 20 '11 at 16:58
1

Xcode9 solves this problem. By default, a new group is created with corresponding folder on disk.

Blazej SLEBODA
  • 8,936
  • 7
  • 53
  • 93
1

I've created an app that fixes this issue and reorganizes your file structure to match what you've got in Xcode. https://apps.apple.com/gb/app/xsaviour/id1449886415?mt=12

Mark Bridges
  • 8,228
  • 4
  • 50
  • 65
0

There's no "make my disk look like my project structure" functionality in Xcode.

You could automate the task using AppleScript, though: You can both examine and change project structure from a script. So you'd just have to write a script that iterates over the project structure and makes the folder hierarchy look the way the project structure does, and adjust where Xcode's file and group references point at the same time.

Chris Hanson
  • 54,380
  • 8
  • 73
  • 102
0

I found out that if you move files around in Xcode and press [CMD] at the same time then the folder structure will match the Xcode structure.

Wizard of Kneup
  • 1,863
  • 1
  • 18
  • 35