0

I am trying to use folders instead of groups for a project. The goal is to avoid creating groups in the project files because those tend to cause merge conflicts in git.

Basically I want to have an Xcode project that respects the file structure on the project directory (like in Android projects) instead of using the reference system.

When I drag the folder into the project, I:

  1. disable "Copy items if needed"
  2. Added Folders: Create folder references
  3. Add to targets: MyTarget

This creates a folder hierarchy (in the project; blue folder icon) as I would expect. I can add/remove/rename files in Finder and see them update in Xcode.

Additionally the folder shows the correct target membership. (I've read that for folders, files get their target membership info from the top level folder. I.e. you don't need to set the target for each file.)

Unfortunately when I try to access one of the files, let's say EventBus.swift, I get the error Use of unresolved identifier 'EventBus'

This seems like a target issue, but I'm not sure why targets would be a problem. Alternatively - perhaps since Xcode isn't managing a reference to the EventBus.swift file...maybe the project doesn't know the file exists at all?

If the latter is true - I'm curious if there is any way to get around Xcode's conflict-prone reference system.

Alex Larson
  • 137
  • 1
  • 7
  • Duplicate, probably, of https://stackoverflow.com/questions/52484234/classes-in-swift-files-inside-folder-references-not-seen-by-xcode-10s-compiler – matt May 20 '19 at 23:41

1 Answers1

0

I'm afraid you're the victim of a misunderstanding. Folder references are for files to be included in the app bundle of the built app, such as images or sound files. They are for files that are to be copied into the built app, but inside a folder within the built app.

You cannot use folder references with Swift files that are intended as part of the app's code; such files need to be compiled to form the built app.

Basically the fact is that you are not in charge of the folder structure of what's inside the project folder. The closest you can get, in modern versions of Xcode, is to use what I call a folder-linked group. This is not the same as a folder reference; it's a group, not a folder reference, but it's a group that corresponds to an actual folder on disk in the project folder. As I've explained elsewhere:

When you make a new group, there's a choice of menu items; for example, in the contextual menu, you might see New Group and New Group With Folder. (Confusingly, the choice will sometimes be New Group and New Group Without Folder.) One creates a group plain and simple; the other creates a folder-linked group.

You can spot a folder-linked group in the Project Navigator by the lack of a triangle in its lower left, and by the fact that it has an actual folder name in the File inspector:

enter image description here

Having created a folder-linked group (in the Project Navigator), if you then create a code file (in the Project Navigator) inside that group, it will be inside that folder on disk.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thank you for your explanation. The clarification regarding the _copying_ of files from folders is extremely helpful. It's a bummer that Xcode doesn't support such functionality - but it is was it is. (Extra thanks too for sharing the link to a similar issue.) – Alex Larson May 21 '19 at 15:44