Search code examples
xcodexcode4

Can Xcode Use "Folder References" for Code?


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?


Solution

  • 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.