Xcode -- move all classes in a group to the finder folder of that group? - iphone

Suppose that group A has its path set to folder A in the finder, and group B has its path set to folder B. I move one of my classes from group B to group A. Annoyingly, the default behavior is that the file remains in folder B in the finder, and Xcode sets the path for the file to something like "../B/ClassName.extension"
Is there a way to change this behavior so that in the above scenario, the file moves to folder A in the finder? Or, alternatively, a relatively painless way to move it afterwards?

There doesn't seem to be a solution to this - This is a similar question, but no one has come up with an answer there. How do i consolidate my xcode project files?
Seems like doing it manually is the only way to go unfortunately!

File grouping in Xcode isn't a reflection of the underlying file structure, nor does it particularly need to be. I've never understood there to be any real logic in where Xcode creates files within a project's directory, but I've never cared much because I can group them manually inside the project (not the filesystem) however I want.

Related

Why in Xcode, Groups can be created, but not real folders?

When doing iOS programming, it is interesting that in Xcode (4.3.2), we can create groups, such as a group call Images, and add files to it (either as a link or choose "Copy items into group's folder"). So it looks like a real folder in the navigator, and it even mentions "copy into ... group's folder", but in fact there is no folder. All added files are in the same location as the .m and .h files.
Why would we want Groups, but not real folder? Is there advantage of Groups over the use of a folder?
This is a pet peeve of mine.
I recommend, making the folder in your file system, where you have your project files, then drag that folder into Xcode where you want the group. It will act like any other group, but now be linked to the folder on the file system. Adding files to that group in Xcode now adds them to the folder in the file system.
Much cleaner way of working and helps when locating files in big projects. Keeps git cleaner too.
Update for Xcode 9:
Once you have the groups in Xcode matching the directory structure on disk, moving a file from one group to another in Xcode will now move the file correctly on disk to match.
Note: In Xcode 10, the default behavior now creates a linked folder when you create a new group. The information below still applies otherwise.
The recommended way to organize files is via groups, because it's more flexible than creating directories, and allows you to create complex file hierarchies while maintaining a totally different file hierarchy on the file system. It's simply an organization tool. That said, it is possible to turn groups into folder references, or create them initially as folder references.
Furthermore, using groups instead of folder references gives you greater flexibility if you need to change where things are in the file system - say you have a common folder of code that you use in a bunch of different projects. Groups allow you to organize the files inside of projects as if they were in the projects, while still maintaining one copy of the code in a central location. And yes, you can do this with folder references as well, but groups are much more flexible if you later want to add other things to the same group but don't want them to be added to that common folder.
If you want to link a group to a folder, click on the group in the project file tree and hit command+option+1. Below the drop down that'll say something like "Path: Relative to Group" on the right, there's a little white square with a grey border around it kinda icon next to the word "none". Clicking on this and then selecting/creating a folder will bind the group to the folder.
Furthermore, when you drag a folder into Xcode, it will ask you if you want it to be a group or a folder reference:
There is a Command Line Tool - "synx"
available in github that do exactly what you need.
It reorganizes Xcode project folder in finder to match Xcode groups in project.
You can find it here:
https://github.com/venmo/synx
UPDATE: XCode 9 supports this feature by default. So, no need to use other tools anymore!

groups and files in XCode vs. project folders in Finder

I still can't get the idea that when I create a new Group in XCode, and build the project, and then return back to finder and notice that the group does not exist but any file to be created under this group will appear in the project's directory in finder but that file is alone without being under any group.
(note: I'm not talking about this directory: /Users/username/Library/Application Support/iPhone Simulator/4.3/Applications/uuid/...)
1) can anyone explain why project's directory hierarchy in finder is different from the project's directory hierarchy in XCode ?
2) how can I make the two hierarchies looks like exactly the same between XCode and Finder? so when I create a new Group in future I can see it in finder.
thanks in advance.
Currently you cannot see the group you create in Xcode in finder. Its just a logical entity for you to organise your projects in a better way. Assuming you will be using xcode to make all the changes (including version control) you can use that categorisation and make sense out of it in Xcode.
However you always have the option of creating a folder in finder(your project workspace) and create files and save them in that folder using Xcode. This is particularly helpful for viewing organized code while browsing through the file system. Also you can group files into folders which you can then use across projects.
So basically it boils down to what your use case is. As for me i am happy using xcode. I usually create a directory to hold images and copy directories for libraries into my project folder so that there is clean organisation in the file system too.
Another helpful method is after you create the group in Xcode, but before you put files in it, check the File Inspector pane on the right with the group selected. Make sure Location is set Relative to Group and then click on the folder icon below that. This will bring up a Finder window. Then, make a directory with the same name, and click Choose. Now, anything you put in that group will go in that folder of the same name.
Simple solution is that you make the require folder in Finder, then go to Xcode. In navigator, right click and select "Add files to" option, add the that folder. It will be added to your navigator as well. Now whatever you add to that group, it will be automatically added to your folder in finder as well.

how to add files with same name in different folders of xcode project

I want to have two files with the same name in different folders in a xcode project. These files are actually code files and not any resource files. How can I achieve this?
Thanks
Pankaj
Well if there .h/.m files you can't have them in the same project, even if they are in different directories.
C does not support namespaces, so every class has to have an unique name.
You can add, but when you load it, the system will pick the first one available.
So, really it will not work the way you expect.

Using resources with the same name in Xcode

Is there a way to add multiple resources with the same name to an Xcode project and have 1 of them take priority over the others?
Example:
I added 2 files, both called icon.png, to an Xcode project. They are on different folders in the file system (Folder1/icon.png and Folder2/icon.png) and on different groups in Xcode. Is there a way to tell Xcode to have Folder2/icon.png take priority over Folder1/icon.png? And if only 1 icon.png exists, then use that one.
Thanks.
EDIT (2010-12-23):
You can have multiple files with the same name in an Xcode project even if they are not in separate folder references, but they are in separate groups. Once compiled, the app bundle (which will be flat with no folders in it), will only have one copy of the file (icon.png). How do you pick which copy of the file to use?
I was told that you can do this for BlackBerry. It works something like this: The compiler will go down the list of files in the project and start adding them to the app bundle. If it sees a duplicate, it will overwrite it (or not), so the files at the bottom (or at the top) will have higher precedence and will be the final bundle.
This can better easily be solved by using folders within a resource bundle in your Xcode project. Take a look here: http://developer.apple.com/library/mac/#documentation/CoreFoundation/Conceptual/CFBundles/Introduction/Introduction.html
Wait a second, since you specifically mentioned icon.png, are you trying to supply different iDevices (iPad, iPhone4, iTouch3...) with different icon images?
If yes, check this out, http://d.pr/W2w0 , Apple has already provided a way for us to finish this task with convenience. All you need to do is following a specific naming convention.
What we do if we have a situation like this is:
We have several subfolders on the file system level of the project dir and add them to XCode into separate groups.
This works well. For actually building, we try to avoid Files with the same Name in one target. Icon.png and iTunesArtwork are only added once in every target.
It just proved to be a Maintainability Nightmare

How do I properly add existing source code files to my Xcode project?

I'm new to iPhone development and I'm still getting familiar with the Mac dev environment, including Xcode. I want to add some 3rd party code to my iPhone project, but when I add the "existing files" to my Xcode project, I'm presented with a dialog box that has far too many options that I don't understand and, as such, my project isn't working. When I #import headerfilename.h, I get a build error that reads headerfilename.h: No such file or directory.
alt text http://joecrotchett.com/images/misc/fileadd.jpg
Can anyone explain to me what all these options mean or give me a link to some documentation that can? I'm having a hard time finding anything in Apple's docs.
Which options do I want to choose to add existing source code files to my Xcode project? I should note that the source code files that I'm trying to add are located in my project/Classes/frameworkname/ directory.
After they're added, do I need to reference this new code directory in my project settings anywhere (i.e. some kind of header file directory variable)?
Thanks so much!
Update: I found the following answers/responses on the apple dev forums that were very useful and helped me fix my issue...
To make it simple :
- if you do not check the copy option, the file stay where it is.
- if you check it, it is copied in your project folders In the first case
(what it seems you are doing) you need
to tell the compiler that the header
files are in another directory :
- project info -> build -> search paths -> User Header Search Path : add
the directory from where you took the
header file Hope this will help
You have discovered the most confusing
dialog box that ever came out of
Cupertino. Six years of Xcode, and
this thing still is partly a mystery
to me. To even get that far, I had to
make many test projects to try and
reverse-engineer what this thing does.
The "Copy" box means that it will copy
the files as they are right now, into
the project. If this box is not
checked, then it just references those
files during a build and copies them
as they are at THAT time. For source
code, you want the Copy box checked.
The "relative to" is a total mystery
to me and I can't help you with that.
I usually leave it however it is
already set. Does it mean relative to
where they are on disk, or the
arrangement in Xcode, or in the
bundle? Who knows. The last 2 radio
buttons SEEM to mean that it will
either re-create the folder structure
of the folder you are adding, or just
put "fake" folders in Xcode that point
to the real folders. This is probably
your problem - you are adding source
code that is not all at the top level,
and when it goes to find it, it does
not re-create the hierarchy. Others
can supply a better way, hopefully,
but what I would do is put all of the
source in one folder and add that,
using the Copy box. Then in Xcode you
can make whatever bogus folders you
want and put the source file names in
those fake folders.
This is from the Xcode user guide:
"The project navigator shows projects, groups, folders, and files:
The project or projects in your workspace window are the highest level
of the hierarchy in the project navigator. Open the project’s
disclosure triangle to see the groups, folders, and files in the
project. Select the project to display the project editor, where you
can view and edit project and target settings.
A group appears in the project navigator as a yellow folder icon. The
group does not represent a folder on disk. Although you can organize
your project in Xcode to reflect the organization of files on disk,
moving files into and out of groups does not affect the content of the
folders on disk, and moving files on disk into and out of folders does
not affect the content of the groups in the project navigator.
A folder is a reference to a folder on disk and appears in the project
navigator as a blue folder icon. The contents of the folder in the
project navigator reflect the contents of the folder on disk. It’s
important to note, however, that the files that appear in a folder
icon in the project navigator are not part of your project unless you
have added them to the project explicitly (in which case they appear
directly under the project or in a group, as well as in the folder).
You can view and edit the files in a folder, but to move files in and
out of the folder you must use the Finder.
A file in the project navigator is a reference to a file on disk."
I think usually the default option is the best one. If you want to add into some group, you can tick on the "Copy Item to Destination group"
You don't have to change any project settings after adding
And if you want to add the whole framework code, please choose add existing framework
Copy items into destination group's
folder (if needed)
Well, only if you want it to copy the files. If you are happy with their existing location and don't want it to copy them, don't select it (that's what I do).
Reference Type:
A total mystery to me
Text Encoding
Self explanatory
Recursively create groups for any
added folders Create Folder References
for any added folders
Leave this as the default and then organize the folders in your Xcode project any way you want.
Add to targets
If you add an additional target to your project, e.g. for iPhone vs. iPad versions, then not noticing that it has by default added the files to just one of them (at random?) can cause headaches.
Also, make sure you add the new header directory to your search paths in Project Settings > Build > Search paths. I think that's what's causing your problem.
Hope this helps, I remember how confusing this was to me at first.
If you want to add an Framwork Bundle, just add is as one. Add -> Existing Frameworks