Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Xcode Use "Folder References" for Code?

Tags:

xcode

xcode4

Like many people, I would love to have Xcode use a folder structure that mirrors the folder-structure on disk. However, I cannot get the code in "folder references" (the cyan folders) to show up in my project targets under "Compile Sources." Is there any way to do this?

I have managed to even add a cyan folder to the "Compile Sources" build phase, but that does not result in the contents of that folder being added.

How can I use folder references for code?

like image 593
Dan Rosenstark Avatar asked Sep 26 '11 14:09

Dan Rosenstark


People also ask

What is create folder references Xcode?

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.

How do you add references in Xcode?

I include them in the project by right-clicking on the project folder in the project view in Xcode, and selecting "Add Files to ...". I then navigate to the folder containing the source files, click on the folder and select "Add." With the options "Create folder references" and "Add to target [target name]".

How do I add folders to Xcode project?

Xcode offers several ways to add existing files and folders to your project: Drag the files from the Finder into the Project navigator. Click the Add button (+) in the Project navigator's filter bar, and choose Add Files to “projectName”. Choose File > Add Files to “projectName”.


2 Answers

The simple (and very unfortunate) answer is that Folder References under Xcode remain broken and buggy, they don't work. Tested 04-Mar-2017 in Xcode 8.2.1

Below is an example exploration so you do not have to waste your time replicating the Xcode failure.

(incidentally buggy Xcode crashed twice while I was producing this example)

Per the question, the overall desire is to use a folder reference in Xcode so Xcode picks up all the files in the folder and includes them in the project, and by proxy will update automatically based upon any Finder or Xcode changes of the folder. In this way 50 projects all leveraging the same set of common source code files do not have to be individually updated when those folders get changed. Below explores how this is broken in Xcode (tested in 8.2.1)

The example: ViewController.m includes NSError+Core.h so we want everything from the folder "NSError+Core" to be added to the project.

enter image description here

NSError+Core.h is in this centrally located development folder

enter image description here

Drag the source folder from the Finder into the Project under the "Support" group (nothing up my sleeves, simple drag)

enter image description here

Xcode dutifully offers to add the drag to the target, note the "Create folder references" is selected, not "Create group references". Note also it is clear that Xcode is offering and is told to add this folder and files to the targets.

enter image description here

Although everything looks like it should work, the compiler does not pick up the header file and a recompile yields the same results... can't find header. Ditching the DerivedData does not help either.

enter image description here

So doing a double check, we check the "Compile Sources" under the project and sure enough, the source file is not there either. Remember, Xcode 'added' it to the target...

enter image description here

So what if we drag it from the folder into the "Support" group...

enter image description here

It offers to add them to the project again?! Note that the settings are identical to the first time they were drug in by virtue of the parent folder drag instead of files...

enter image description here

And now the source file shows up in the "Compile Sources" list. Note the bizarre double listing of the files in the project.

(Xcode crashed shortly after snapping this screen shot)

enter image description here

And of course the compiler can now find the header file and the error clears on the import as it should have the first time we drug it in...

enter image description here

Did it just need a little help to "find" the file? If so, the "Create folder references" does exactly what?

So we try and tidy up and drag the files back from the parent "Supporting Files" group to their rightful folder. Without any confirmation, indication, notification, the files just vanish from the group and nothing happens in the NSError+Core folder.

enter image description here

Oh by the way, it really did delete them from the project too... The Compile Sources no longer has the NSError+Core.m reference.

enter image description here

SO to sum up, "Folder references" as implemented thus far do not seem to have any useful purpose... It would appear to be a 6+ year old dunsel on the USS Xcode.

like image 84
Cerniuk Avatar answered Sep 28 '22 07:09

Cerniuk


Kevin - linked source folders, folder references, etc, are super useful for when you have a common code base shared across different IDEs, like I do for my games that I compile on Windows, Mac, iOS, Android, Linux, etc. I have 3 different IDEs all building the same shared codebase, so it's very helpful when one automatically picks up on a new file and adds it right into the project after I merely run svn update, and I svn commit from one IDE (say XCode) and my Eclipse in Windows project picks up the change. I have a different project for each because each IDE likes the project files in a certain configuration so it's easier for me to have multiple SVN directories (base-project, project-ios, project-android) that all share code in base-project than to have one mega project directory with the different IDE bits unhappily all shoved into subdirectories (which is what I tried the first time around).

Furthermore - it used to work fine in XCode 3. They seemed to not like that useful of a feature so it is no longer working in XCode 4 as I've just found out.

like image 39
rbgrn Avatar answered Sep 28 '22 08:09

rbgrn