Xcode organization in finder? - xcode

I have read all the suggested StackOverflow posts on this question. It bothers me that Xcode will not organize my files in the finder the same way it does in the editor view...it will only do that if I copy files in from an external source and specifically tell it how I want things organized. Is there a way to make Xcode have the Finder respect the same organization as a default? I'd love to create a directory and then a file in that directory, and see the changes in both places.
I hate opening a project and seeing ALL my files in one place.

If you want your Groups structure in Xcode to mirror your Directories structure in the file system (which is also the Folders structure in the Finder), you have to take the following steps when adding files that you want to go into Groups/Subdirectories:
Create the folder in the Finder (or the directory from the command line)
Drag that folder into your Xcode project. Import it as a Group. Make sure its reference style is Relative to Enclosing Group, and that you drop it into the Group that represents its parent directory.
To add new files, select the Group and choose Add Files. The files will be stored in the directory that that Group represents, and they will be within that Group in the Xcode UI.
If your files are not yet under SCM control, one thing you can do is just delete them all from the Xcode project, rearrange them on disk however you want, then drag all the folders and files back in, making sure to not make copies, to create groups, and to set the reference style to Relative to Enclosing Group.

Related

Create groups vs Create folder reference in Xcode

I'm curious when using Xcode.
I always need to choose an option when copying the open source class files I need for my project.
The options are "Create groups" and "Create folder references".
I've read the difference between the two already.
But it is not understood at all.
When I chose one of the two options, I deleted the file from my project and the open source class file was deleted at the same time.
I do not want it.
I want to always exist as an independent file. What should I choose?
It took me a while to understand the difference between those two options so I thought I'd share:
I'm writing this when Xcode 12 is the current version
The biggest difference is how the imported folder / file will be accessible from the finder. Let's say you want to import a new folder to your Xcode project. When you choose Create groups Xcode will import your new folder and create a group (the yellow icon). When you go to your project's directory (using Finder) you should see a folder named like the one you've just imported, but any additional files you put to this folder trough Finder won't be visible in your Xcode project.
When you choose Create folder references the imported folder will have a classic blue folder icon in Xcode. This new folder will also be visible when you go to your projects directory, but now every new file you put there will also appear in your Xcode project.
If you want to know more just read this explanation http://www.thomashanning.com/xcode-groups-folder-references/
It's pretty straightforward and easy to understand.
There are two types of any Xcode project structure organization:
filesystem project structure (inspectable with Finder.app)
IDE project structure (inspectable with Project Navigator in Xcode's navigator area.)
Xcode relies on filesystem organization by keeping a reference to a "physical" file or folder. That is why you may have all the classes, images, plists, and other parts of your projects stored in the same folder, while the references to these parts are being kept organized into respective groups within Xcode project.
When you create a group, it affects the organization of your project in Xcode. When you create a group with a reference folder, it creates both, a group in Xcode project and a folder in your filesystem.
However, since Xcode 9 it seems like, Apple enforces filesystem project structure to correspond to Xcode project structure. It means, that you explicitly has to choose "New Group without Folder"
Added files and folders from Finder into the Xcode group. Xcode 10.2.1. Just take a look at the examples without Copy items if needed
Files
Common behaviour for create groups and create folder references
Rename a file from Finder does not rename the file in Xcode, as a result Xcode doesn't handle it and it will mark them by red color.
Rename a file from Xcode rename the file in Finder
Change a file from Finder change the file in Xcode and vice versa
Remove to trash a file from Xcode change the file in Finder and vice versa
Add a file from folder in Finder does not add the file to group in Xcode
Folders
Common behaviour for create groups and create folder references:
Rename a folder/group from Xcode rename the folder in Finder
create groups
If you choose create groups, then the corresponding folder appears with a yellow icon in the project navigator.
It is just a virtual folder. It useful during development when you copy a file and it is not compiled and you do not have a time to make changes in it and want only to check if a project works. So you can just remove a reference from a file. It will not be removed from a folder as a result you will be able to add this file again.
Rename a folder in Finder does not rename a name of group in Xcode. You steal can work with group but previous content inside although visible but unavailable
Add a file to a folder in Finder does not add the file to group in Xcode
Add a file to a group in Xcode does not add the file to folder in Finder
Remove a folder in Finder does not remove the group in Xcode. You steal can work with group but previous content inside although visible but unavailable
create folder references
If you are choosing create folder references, then the folder appears with a blue icon in the project navigator.
It is a real folder and any changes will be reflected
Rename a folder in Finder does not rename a name of folder in Xcode. The previous content is gone and you can not work with this folder
Add a file to a folder in Finder adds the file to folder in Xcode
Add a file to a folder in Xcode adds the file to folder in Finder
Remove a folder in Finder removes the folder in Xcode.
[Copy items if needed]
Create folder references:
Useless because even if you list this folder to be included with a target, Xcode won't include any of its contents in that target and there's no way to click on something in the folder and see which targets it's included in.
Create groups:
This is the only way to go, if you want to include any of the folders' contents in a build target.

xcode 4.x.x Project Navigator - use real folders

Is there a way to make xcode use real folder hierarchies in the Project Navigator?
I'm finding these pseudo folders somewhat annoying because they do not reflect the project on disk.
Or at least have the xcode-made-folders a different color than the real folders?
Thanks.
Not really, although they are colored differently.
You have groups (yellow) -- which can refer to a directory, but does not update automatically to reflect what's on disk. Groups can also represent a group which has no relation to a directory or the structure of its contents.
You also have a reference (blue). This updates automatically, but its contents are not first class project items.
Personally, I just structure projects such that they can be (re)constructed from an on-disk representation easily (e.g. drop the folder), or in another IDE. Quite often, details which could (unnecessarily) be defined in Xcode are moved outside of the xcodeproject. Synchronizing the representation in the Project Navigator is about as easy as drag and drop once you work out how to structure the program/sources/targets.
Edit: I am referring to the Project Navigator. Whether what you refer to as the "project organizer" means the Project Navigator or some part of the Organizer, I am not sure.
Making a folder structure mimic your Project Navigator group structure:
For this procedure, as always, make a backup of your whole project folder before doing this (a zipped Archive in Finder does nicely).
What you want is easy to set up for new files added to or created by the project, but it can be a mess if you already have a lot of files in the groups you want, but not in the disk folders you want.
If you hilite a group in Project Navigator, move your attention over to Utilities pane > File Inspector > Identity. The key area is dealing with Paths. The popup will give you various relative paths to the default location of the group's files, and Full Path will always give you just that. In a fresh project you'll find that any groups created all have the same location assigned.
Now the key here is the little window icon button just under the popup (to the very right). If you click that you can choose a (folder) location for files in that group. If you already have files in that group they will not be moved to the new location, but will instead turn red in Project Navigator because the files on disk are not in the newly assigned path for the group, but moving them into the folder you just set up for the group should fix you back up.
(Tip: The little arrow button is a shortcut to "Reveal in Finder")
Now, when you make a "New File...", after selecting what type of file you want (Next), then configure whatever setting apply for that type (Next), you're presented with a sheet asking where to place the new file(s). If you have that group selected in Project Navigator before you begin this process, it will already be selected as the item in the "Group" popup at the bottom of the sheet, and the folder associated with that group will be selected in the file browser. If you decide you want it in a different group (I do that all the time because I won't have the proper group selected in Project Navigator before I get to this point), just change it in the Group popup at the bottom and the file browser will change to the folder associated for the group you just selected.
If you later make a sub-group of a group in Project Navigator, it will default to the path of the parent group. If a sub-group already exists when you set the path of the parent group, the paths for any existing sub-groups will not move with the parent.
Avoid duplicate files: If you want to move existing files into another group (that already has a path assigned to it) and also move the files into the group's folder on disk, you have a choice at this point. You can go ahead and delete them from Project Navigator, making sure you only delete the reference to them and move on to the next step, or as an option, you can actually move them in Project Navigator to the group you want them in (this doesn't change disk location of the files -- the folder associated with the group only applies to new files for the group) and use them as a guide for what still needs to be dragged in from the Finder. This can come in handy if you are going to be moving a lot of existing files around, as you'll see in a moment.
Now, in Finder, manually move some files into the folder that they should end up in (the folder associated with the new group). If you moved them over in Project Navigator (instead of removing them), they will turn red because the actual files are not at the place they were, but this can be a guide (especially if you're moving a lot of files around) for what still needs to be set properly.
Now, you can drag and drop them from Finder onto the group that's associated with the folder where you put the files. In the dialog that comes up, it doesn't matter if the checkbox is checked or not for copy (if necessary), since they are already in place, but it IS important to make sure you have the "Add to targets" checkbox(es) checked.
If you deleted the files (remove reference only) from the old group you can go into the old group's folder in Finder and drag the files from there to the new group in Project Navigator, you will need to make sure Copy if necessary is checked, but this is where end up with duplicate files on disk… In both the old group's folder and the new group's folder. This is why you want to move them to the new location first in Finder before dragging them into the new group in Project Navigator.
If you left the red files in Project Navigator sitting in the proper group, after you add the same files onto the group (copy to group not necessary since they're already in the proper location) you will end up with new (black) references to the files as well as red broken references to the same files. At this point you can just delete the red files from the group without being asked if you want to delete the files or just the reference. Leaving the red files in the new group until you have the proper (black) files showing up alongside them in Project Navigator can help to make sure that you get all of your files back into the Project Navigator.
If you're only shuffling around a couple/few files, then once you have the files moved in both the Project Navigator and in Finder to the proper locations, you can select each red reference in Project Navigator and choose the path for that particular file (in the same manner as you assigned a folder to a group). But, if you're moving around a bunch of files, this can take a long time (you can only assign one at a time), so it's better to just drop them in from Finder.
One more gotcha to look out for… It's best to just leave everything alone that sits in the "Supporting Files" group, as well as MainMenu.xib, since these are all expected to be in specific locations according to the Build Settings.
Remember: Always make sure you have a backup first (things can get out of hand if you are shuffling a LOT of files around)... Just having a snapshot probably won't help since reverting to a snapshot won't move your files on disk back to where the originally were.

Why can't I delete files from my XCode project?

This is probably a very easy question, but I'm having trouble deleting resources from my XCode project. I added them using "Create Folder References for any added folders" so that I could import a whole offline HTML site with its correct folder structure.
Unfortunately, now it has been added like this I don't seem to be able to delete individual files in the structure (it's not available from the Edit menu).
Can anyone help please? Thanks!
That isn't how folder references work. The idea is that its only a reference, you can open files within it and save it from those editors, you can delete or move the entire reference throughout the xcode project, but you can't actually edit it - its read only as far as xcode is concerned. Likewise, you cannot restructure it (move internal files around).
I'm not to sure why apple decided to make this the case, but apparently they have.
If you want to know how one might use the xcode folder system, here's how I tend to use them with my projects:
Whenever I subdivide code into folders, when I drag them into my project I click "recursively create groups for any added folders". If you do this, you any changes you make within xcode will not reflect the actual file itself. As far as I know, there is no way to do this. What does happen then is that when you add a new code file to it, the directory starts off in that file by default. ie, you don't need to navigate to it manually when you create a new file.
I use folder references whenever I'm working with content for an application I'm using. This way, I add all my images, folders, configuration files, whatever - and xcode immediately lists them. The reason I have it within xcode, I can I copy the files into the executables directory by dragging the folder reference into a "Copy Files" build phase.
Thats basically (to my knowledge) how one uses the folder types within xcode - sadly, I don't know how to achieve the functionality you want. You may have to manually delete the folders in finder, which if you do use folder references will update xcode to the change.
I ran into the same issue by using "Create Folder References for any added folders". I wanted to change some of the times but that's not possible. I had added a main folder that had other directories under it. I just had to select the main directory and deleted it and then just add the subdirectories that I needed. You can't make any location or removal changes to the directories that are added this way. -- Jeff
In the project browser, where you're looking at files, right click and choose "Delete". It'll prompt you to either remove the file from the project (leaving the underlying file on the filesystem) or to also move the underlying file to the trash.
I ran into the same issue. Delete the files from the folder directly as opposed to from within Xcode. You'll see the entries turn red under your project. Restarting Xcode should make these red entries vanish.

How should I organize my Xcode project files?

I'm trying to wrap my head around Xcode's file organization - or lack there of. I can do all I want in project and it looks great with all the "fake" folders and structure. I go look at the file system and boom HUGE mess. I've tried importing files with the Create Folder Reference for any added folder option checked and that works, kinda. I get the structure I want both in Xcode and on the filesystem.
Issues: When I add a file to a folder on the filesystem that is a Folder Reference in Xcode, its not in Xcode when I go look, not even after reloading the project. Files/Subfolders in a Folder Reference can't be moved around in Xcode. When I move them on the filesystem I get red links (can't find the file?) in Xcode.
How do I keep a organized project and filesystem? How can I set up a project to just recognize a folder and show its (current and up-to-date) files and subfolders in my project?
Another issue I seem to run into, if I use a Folder Reference and change a file, the file is not updated in my application unless I do a full clean & rebuild. If I don't use a Folder Reference, all my files are dumped into the Resource folder of the application bundle, not in the nice structure I have in my project.
Should I care at all? Should I just use the fake folders and let everything go everywhere and not care? My application bundle will be a mess, the filesystem will be a mess, but it will all work... I would hope?
Edit:
My biggest reason for wanting an organized filesystem is that the resource files (images, sounds, other datafiles, etc.) are not edited in Xcode. I have to access them in 3rd party apps via the filesystem. If its a mess things are harder to find and maintain in the other 3rd party applications.
Also what happens if I want a structure like the following:
Images/Backgrounds/Name.png
Images/Icons/Name.png
Images/Titles/Name.png
Should I use long filenames rather than folders to organize?
Images_Backgrounds_Name.png
Images_Icons_Name.png
Images_Titles_Name.png
I also wish Xcode automatically kept itself and the file system in sync.
So much so, that I spent an hour doing so manually for a project called acani-iphone on GitHub. Basically, I just moved some of the files around using Finder, creating new folders as I pleased. Then, I switched back to Xcode and saw that the files I just moved were now red (because Xcode was thinking they're where I moved them from and so couldn't find them).
UPDATE: I just figured out that I could've then just clicked on the red group or file, pressed CMD+i (Get Info from the context menu, which you can open by right-clicking on the red file or group), and under the General tab, clicked Choose, then found where I moved the file to in the filesystem. But, I didn't do that, here's what I did instead, which also works:
Then, I just highlighted all the red files in Xcode and pressed command + delete to delete the broken (red) references. Then, I right-clicked on the Group I wanted to add the files to (usually the same group), and clicked Add > Existing Files.... Then, I found the same files in the new spot on the file system. I kept "Copy items into destination group's folder (if needed)" unchecked, I checked the radio button "Recursively create groups for any added folders," and I checked add to target acani if the files I was adding were being used to build the acani iPhone app.
I did the above with like a directory of files at a time. A few times I was more aggressive, adding multiple directories at a time, since I almost always selected the radio button "Recursively create groups for any added folders."
I found out that the files acani_Prefix.pch and acani-Info.plist had to stay in the root file system dir (although there may be settings you can set to allow these files to be elsewhere, like I think you can add a line to acani-Info.plist so that you can move/rename acani_Prefix.pch, but I'm fine with them in the root dir on the file system.
That was annoying to do, and perhaps not even worth the trouble, perhaps procrastination, but going forward, before adding existing files to Xcode, I'll first make sure they're in the place I want them to be on the file system.
OK, so here is how it works:
Xcode doesn't know about any files until you tell it about them. That is, even if you add a file manually in the finder (usually a bad idea) to a folder that contains files in an Xcode project, it doesn't know about them until you "add existing file to project".
The best practice (imo) for adding an existing file (or group of files) to a project (say, some code you just downloaded) is to choose "add existing files" and then "copy items to destination group's folder (if needed)" in the next dialog, if you want your project to have a copy of the files in question, rather than merely a reference to them (there are advantages and disadvantages of both).
Don't worry too much about the naming of folders in Xcode, or where you put things, but try to keep to a standard that makes sense in your environment. For example, I always put the classes I write in "Classes", and have separate folders for any library code i've downloaded for use in the project. I always put images/icons/audio etc in to "Resources".
In short, if you like what's in the project folder to be approximately the same as what's in your project, always add existing files by choosing the "copy items to destination group's folder"
The flexibility in XCode is intentional. It's up to you to decide how you like to organise things.
Should I care at all? Should I just use the fake folders and let
everything go everywhere and not care? My application bundle will be a
mess, the filesystem will be a mess, but it will all work... I would
hope?
IMO no... :) basically. The whole point is that XCode has been designed to give you the best experience of programming. If Apple wanted you to physically organise all your files and folders within the actual filesystem then they would have made it that way.
I don't really understand why you would want to organise all the files and folders in this way anyway? It makes no difference to the running of the application and the "fake" folders (groups) in XCode adequately provide the necessary visual aid for yourself (and others) to navigate through your classes and other resources. Organising it correctly in your filesystem (as you have found) surely just makes things more difficult?
Use Synx.
It rearranges your files on disk to match your Xcode groups. I try to run it before committing any code that changes the Xcode groups, and it keeps the project nice and tidy.
It would be great if Xcode could keep itself and the file system in sync. Unfortunately it doesn't. One reason for wanting it to is so the hierarchy in your SCCS matches the one in Xcode.
I fall back to keeping things organized in Xcode, and leaving the file system separated into not much more than "Classes" and "Resources".
This changed with Xcode 9. From the release notes:
Groups in the Project Navigator are now more closely associated with
directories in the file system. (28612132)
Dragging files between groups in the Project Navigator moves the files in the filesystem and updates any associated SCM working copies.
When a group is connected to folder in the filesystem, creating, renaming, and deleting groups updates the corresponding files and
folders in the the filesystem.
To remove a connection between a group and a folder in the filesystem, select the group, and then open the File inspector and
click on the on the Clear path button (X).
To add or update an association from a file or a folder in the filesystem to a file or a group in the project, select the file or
group, open the File inspector, and drag the corresponding file or
folder onto the Location section in the File inspector.
The new behaviour is available from the 'New Group with Folder' command (which may appear as just 'New Group'), while the old behaviour is available from the 'New Group without Folder' command (which may also appear as just 'New Group'!) The dominant usage amongst any existing groups in the target folder seems to determine which command gets labelled 'New Group'. It's more than a little confusing, but if you are in the habit of choosing one or the other, the idea seems to be that you can just stick with the default 'New Group' command. (See rob mayoff's far more thorough explanation.)
What I do is create a group to represent each folder and then, before adding files to it, in the right panel, first tab, immediately below "Path", there is an icon that allows you to choose the folder. In that folder dialog, I create a folder that matches the group and choose it.
In xcode3, this resulted in new and add files dialogs starting in this path. That made it worth the effort. Xcode4, however, does not respect this setting. Therefore, its questionable whether there is any real value in it. I also wish XCOde would support better file system organization.
Considering that file names must be unique within a project, regardless of groups and folders, there is justification for accepting the flat folder structure default and using groups for IDE convenience. Its difficult to come from other platforms where this is frowned upon.
i feel you and personally cannot NOT care about the actual structure and just rely on workspaces.
what would be really great is a tool that will go over the workspace structure and re-organize the file system accordingly, taking care of any re-naming of folders etc. this would be a classic solution and IMHO should be implemented as an option as we re-organize our project as we move about it.
some issues could be source control though xcode4 works with both git and SVN.

Xcode File management. What is best practice?

I've been using Xcode for a while now. One thing that always bugs me is the way it handles files. I like to have my files all in nested folders rather than one big physical folder, but when you create a group in Xcode by default it does not create a folder just a virtual folder within the project.
I can see that virtual folders are great for linking code in arbitrary places into your project but once you get beyond a few classes I find the one big folder approach really painful. And then if you try to fix it later it takes ages and is easy to break your build.
Is it possible to change this behaviour so that by default it creates a physical folder? Or am I doing it wrong and trying to cling to some other way of working? How do other people work with files in Xcode?
1) Remove all the files from the project. Select All and press Delete, and click the Delete File References button, not the Also Move to Trash one.
2) Go to the Finder or Terminal and rearrange your files in folders to your heart's content.
3) Select all the top-level files and folders and drag them back into your project. When given the choice, choose Create Groups for Added Folders.
You'll now have a group hierarchy that follows the directory hierarchy on disk.
You'll need to make sure all the proper files get back into their correct build phases and targets. It's best to do this one target at a time in a multi-target project.

Resources