Classes in Swift Files Inside Folder References Not Seen by Xcode 10's Compiler

Classes in Swift files inside folder references not seen by Xcode 10's compiler

It works well for bundle and data files, but not for source code.

Yup, well, that's because those are completely different kinds of thing, and need to be treated in completely different ways.

You can't put code inside a folder reference (blue). That would simply mean "copy this code file into the body of the app", which would be silly. That's the kind of thing you would do with bundle and data files.

If the problem is to organize your code file references within the project window, you can use:

  • a group (yellow with a red mark in the lower corner)
  • or, a folder-linked group (yellow plain and simple)

In the latter case, the code file itself will also be placed in the corresponding folder on disk inside the project window, so this is also a mode of organization on disk.

NOTE But note that you must always let Xcode itself organize the contents of the project folder! You must work entirely within the Project inspector in the project window. Stay out of the Finder. Otherwise, you'll break your project.

Why can't I access files that are in a folder (as opposed to a group)?

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:

Sample Image

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.

Adding Folder to Xcode Project is not Properly added

If your Xcode project looks like this…

Xcode project with folder reference

…then you've added your ViewControllers folder as folder reference.

To change this folder to a group follow these steps:

  • Right click on ViewControllers
  • Choose Delete in the context menu
  • Choose Remove Reference in the dialog that popped up. The folder will be removed from your Xcode project but not from the file system.
  • Then choose Add Files to <YourProjectName>… in the File menu
  • Find your ViewControllers in the file system
  • Before clicking Add make sure that the option Create groups is selected
  • Click Add

You've added your ViewControllers folder as group:

Xcode project with folder group


Important note regarding Xcode 8:

The option Create groups might not be visible right away. Apple moved this in Xcode 8 to the bottom of the Add Files to… dialog:

Collapsed options

This reveals the option Create groups:

Expanded options

Xcode added folder in blue and related there files are not compiling

If you simply drag drop a file and choose Create Folder reference, it will add as a blue folder.
Please make sure you select "Create groups" from the dialogue and make sure you have selected all the targets in the "Add To Targets" section.

Like this

Sample Image

It will be added as yellow
Sample Image

Group-> With groups, Xcode stores in the project a reference to each individual file. This can lead to problems:

Folder references-> Folder references are simpler. You can spot a folder reference because it shows up in blue instead of yellow.

How to use class/function in another swift file which in same folder without Xcode

If you're not using xcode, I assume you're running them either via swift or swiftc. In either case, just list both files on the command line and they will be treated as the same module. Your L.swift file should have a main() function that runs the tests.

Xcode won't recognize a new Swift class

In the Project Navigator on the left, select the file where you declared RecordedAudio.

Now, in the File inspector on the right, look at the Target Membership. The app target (not the Test target) should be checked, similar to this:

Sample Image

If not, check it! And now all will be well.

Can't make files or folders inside main Xcode project folder

in the second step of creating the file, do you still have selected the right folder?
Or is there maybe an other folder selected?



Related Topics



Leave a reply



Submit