I have built two frameworks in swift, let's call them CoreFramework
and MyFramework
MyFramework
has a dependency to CoreFramework
and uses some of CoreFramework
's classes, structs and enums in its public methods, like this for example:
public func fetchData() -> CoreStruct
I have set up a podspec for both Frameworks and I can use MyFramework
as a pod in my project. In my project I would write something like:
let result = fetchData()
This compiles and Xcode even gives me the right type when I alt+click the variable, but if I want to explicitly specify the type of result
like this:
let result: CoreStruct = fetchData()
I get a compiler error and I have to import CoreFramework
What do I have to do, to be able to explicitly use things like CoreStruct
in my project, without having to import the underlying framework?
There is no way in Swift to make importing one module automatically import another. That was a conscious choice on the language designers’ part, and Swift is strict about it.
There is something called an “umbrella framework” which sort of does what you want by letting you create a facade for several subframeworks, but Apple specifically discourages you from creating one.
Barring that, you must ensure that (in your example) fetchData()
and CoreStruct
are compiled into the same framework, which you could do by:
MyFramework
include CoreFramework
’s code as a git submodule,MyFramework
use Cocoapods to build CoreFramework
within the same workspace (i.e. publish CoreFramework
as a pod, and include it in MyFramework
without the use_frameworks
option in the podfile, so that you get two projects in one workspace compiled into one framework),MyFramework
and CoreFramework
to the same project),…or anything else that causes the two source trees to be compiled into one framework.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With