44

I'm curious when using Xcode.

I always need to choose an option when copying the open source class files I need for my project.

The options are "Create groups" and "Create folder references".

I've read the difference between the two already.

But it is not understood at all.

When I chose one of the two options, I deleted the file from my project and the open source class file was deleted at the same time.

I do not want it.

I want to always exist as an independent file. What should I choose?

Lucas
  • 523
  • 2
  • 10
  • 20
usinuniverse
  • 690
  • 1
  • 9
  • 15
  • _"When I chose one of the two options, I deleted the file from my project and the open source class file was deleted at the same time."_, could you please be more specific here? What exactly did you do that caused such behavior? – Yevhen Dubinin Dec 14 '17 at 09:12
  • @YevhenDubinin I can not remember which of the two options I chose. However, if there is a "1" number and a "1 (1)" copy of the number "1" I removed the duplicate "1 (1)" but deleted it to "1" original. So I want the copy and the original to exist independently. Which option should I choose? – usinuniverse Dec 14 '17 at 10:11
  • 2
    I just want to know the difference between the two options. It does not matter what I did. – usinuniverse Dec 14 '17 at 10:17

4 Answers4

37

It took me a while to understand the difference between those two options so I thought I'd share:

I'm writing this when Xcode 12 is the current version

The biggest difference is how the imported folder / file will be accessible from the finder. Let's say you want to import a new folder to your Xcode project. When you choose Create groups Xcode will import your new folder and create a group (the yellow icon). When you go to your project's directory (using Finder) you should see a folder named like the one you've just imported, but any additional files you put to this folder trough Finder won't be visible in your Xcode project.

When you choose Create folder references the imported folder will have a classic blue folder icon in Xcode. This new folder will also be visible when you go to your projects directory, but now every new file you put there will also appear in your Xcode project.

If you want to know more just read this explanation http://www.thomashanning.com/xcode-groups-folder-references/

It's pretty straightforward and easy to understand.

Bartosz Kunat
  • 1,485
  • 1
  • 23
  • 23
  • 4
    It's also worth noting the differing behaviour when adding resources to a project. If you resources in a Group, each item in the Group is copied to the root of the Bundle in a build. If you create a Folder Reference, a folder of the same name, containing the resources, will be created in your Bundle – the folder structure is maintained in the Bundle. – Giles Nov 11 '20 at 09:40
  • I'd recommend going with folder references for library imports in Xcode, so that files get automatically updated/deleted with every version. – Pranav Kasetti Oct 10 '21 at 23:50
  • In my case, I was copying the entire contents of one project over to another, and the code files nested within groups in the origin project were not visible outside those groups in the destination project unless `Create groups` was checked. – shoe May 24 '22 at 02:37
16

There are two types of any Xcode project structure organization:

  • filesystem project structure (inspectable with Finder.app)
  • IDE project structure (inspectable with Project Navigator in Xcode's navigator area.)

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.

When you create a group, it affects the organization of your project in Xcode. When you create a group with a reference folder, it creates both, a group in Xcode project and a folder in your filesystem.

However, since Xcode 9 it seems like, Apple enforces filesystem project structure to correspond to Xcode project structure. It means, that you explicitly has to choose "New Group without Folder"

Christopher Graf
  • 1,929
  • 1
  • 17
  • 34
Yevhen Dubinin
  • 4,657
  • 3
  • 34
  • 57
  • 5
    I still do not understand. In other words, when dragging and dropping a folder into a project, what options do you need to have in order for the original and the copy to exist separately? – usinuniverse Dec 14 '17 at 11:17
12

Xcode Create group vs Create folder

Added files and folders from Finder into the Xcode group. Xcode 10.2.1. Just take a look at the examples without Copy items if needed

Files

Common behaviour for create groups and create folder references

  1. Rename a file from Finder does not rename the file in Xcode, as a result Xcode doesn't handle it and it will mark them by red color.
  2. Rename a file from Xcode rename the file in Finder
  3. Change a file from Finder change the file in Xcode and vice versa
  4. Remove to trash a file from Xcode change the file in Finder and vice versa
  5. Add a file from folder in Finder does not add the file to group in Xcode

Folders

Common behaviour for create groups and create folder references:

  1. Rename a folder/group from Xcode rename the folder in Finder

create groups

If you choose create groups, then the corresponding folder appears with a yellow icon in the project navigator.

It is just a virtual folder. It useful during development when you copy a file and it is not compiled and you do not have a time to make changes in it and want only to check if a project works. So you can just remove a reference from a file. It will not be removed from a folder as a result you will be able to add this file again.

  1. Rename a folder in Finder does not rename a name of group in Xcode. You still can work with group but previous content inside although visible but unavailable
  2. Add a file to a folder in Finder does not add the file to group in Xcode
  3. Add a file to a group in Xcode does not add the file to folder in Finder
  4. Remove a folder in Finder does not remove the group in Xcode. You still can work with group but previous content inside although visible but unavailable

create folder references

If you are choosing create folder references, then the folder appears with a blue icon in the project navigator.

It is a real folder and any changes will be reflected

  1. Rename a folder in Finder does not rename a name of folder in Xcode. The previous content is gone and you cannot work with this folder
  2. Add a file to a folder in Finder adds the file to folder in Xcode
  3. Add a file to a folder in Xcode adds the file to folder in Finder
  4. Remove a folder in Finder removes the folder in Xcode.

[Copy items if needed]

yoAlex5
  • 29,217
  • 8
  • 193
  • 205
9

Create folder references:

  • Useless because even if you list this folder to be included with a target, Xcode won't include any of its contents in that target and there's no way to click on something in the folder and see which targets it's included in.

Create groups:

  • This is the only way to go, if you want to include any of the folders' contents in a build target.
scaly
  • 509
  • 8
  • 18
  • That's what I found as well. The target membership selection is vital, so first I added the directories on the filesystem where I wanted, then I created groups (right click, create group) named the same in Xcode, then I added the files one by one from the folder to the group by drag-and-drop. Now Xcode matches my filesystem but the files may have their target membership specified precisely. This lets me have a different firebase GoogleService-Info.plist for each target, and one target per firebase backend (dev, prod, etc) – Mike Hardy Nov 17 '21 at 19:15