Swift files in Bundle

I wanted to move a few .swift files from the framework to different container. I tried creating framework/static-library/SPM to move a few files from the framework but getting compilation errors for the new framework and old framework due to dependancy. I don't want ondemand resource and wanted to keep it local. So I looking for some container to move a few files from framework and load/import them back. I tried moving those files to Settings.bundle but unable to load/import them in the framework? Is there any other way to achieve this?

Replies

When you want of separate some source files this way, you generally need to create a module with the files, so that your other code — the code that's using the declarations in these moved files — can use an import statement to grab the "stuff" that you moved.

If you create a framework target or a static library target in Xcode, then building those targets will build a module of the same name that you can import into other code. If you make a Swift Package, then adding the package to your project will cause Xcode to create a module for the package, which you can again import.

That's a very general overview of how this is best done. If you have any more specific questions, ask away…

@Polyphonic

You said "you generally need to create a module", I believe you are asking to create a framework or SPM or static library. If not, then how to create the module .

I tried creating framework/target/SPM but facing compilation errors due to dependency.

I have a framework with 50+ classes and I want to move 4 files outside of this project. Let's say, I have to move Class A,B, C and D out side of the framework. These 4 classes are accesing other few classes from the main framework and more main framework is accessing Class A,B,C and D. So getting compilation error. I want some sort of container(other than folder) to keep those 4 files separate locally and access them in the main framework.

Creating the module is the easy part, because Xcode typically creates modules for you, when it compiles the target (framework or static library) or package (SPM).

However, it sounds like you might have circular references between the files you're leaving in the original framework and the files you're moving to the new framework. That won't work, because Swift can't create either module without previously having compiled and created the module for the other framework. That's impossible.

By contrast, the circular references are not a problem when all the files are in the same framework. In that case, Swift can partially compile each file, to find out what's declared in each, and it can use that information to finish the compilation of each file without getting stuck in an endless reference loop.

That's not possible across module boundaries, though, since Swift can't build a module until it has fully compiled each file in the module.

In other words, if you break off a piece of framework X to make a new framework Y, X can depend on Y, but Y can't depend on X.

OTOH, if A, B, C and D in framework Y don't need anything from framework X, you should be able to get this to compile simply by importing Y in the files of X that refer to A, B, C or D.

Thanks @Polyphonic.

A, B, C and D in framework Y is dependent on framework X and vice versa. That’s why l am looking for some kind of container instead of a framework to separate those files from framework X. I tried moving those files in .bundle but it didn’t work either.