Can Xcode Use “Folder References” for Code?

前端 未结 4 915
不知归路
不知归路 2021-02-06 22:14

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

相关标签:
4条回答
  • 2021-02-06 22:44

    I just tried doing this to share code across multiple Xcode projects, and our team came to the conclusion that it's better to create an Xcode project that contains all of your shared classes, compiles them into a static/dynamic library, and then add that as a subproject to those that need the shared code. Then you can set up target dependencies and link your shared library. This will get you the "automatic updating" every time you add a new class to the shared library project.

    This approach also works well with submodules or even cocoapods/carthage.

    0 讨论(0)
  • 2021-02-06 22:46

    You can't. Why are you even trying? A folder reference's job is to embody a folder, without having entries for all the individual files in the folder. It's primary use is for copying an entire folder of resources verbatim into a project. If you want to compile the sources though, then those sources must be referenced in the Compile Sources build phase of the target, which requires having individual entries for each file.

    Depending on what's in the folder, it might make more sense to have a Makefile or some other external build process that builds the content in the folder into a static library. This way you can invoke that build process from a Shell Script Phase, and then just link in the resulting static library. Alternatively if you're using this folder as a way to have a shared bit of code (e.g. an svn:externals or git submodule), you could give that folder its own Xcode project and then embed that project into any of your other projects which share this folder.

    0 讨论(0)
  • 2021-02-06 22:47

    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.

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

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

    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.

    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.

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

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

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

    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)

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

    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.

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

    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.

    0 讨论(0)
  • 2021-02-06 22:55

    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.

    0 讨论(0)
提交回复
热议问题