Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expose Classes of Dependencies in Swift Framework

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?

like image 690
eyeballz Avatar asked Oct 21 '15 09:10

eyeballz


1 Answers

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:

  • having MyFramework include CoreFramework’s code as a git submodule,
  • having 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),
  • merging the projects (i.e. just add all the source files from MyFramework and CoreFramework to the same project),

…or anything else that causes the two source trees to be compiled into one framework.

like image 91
Paul Cantrell Avatar answered Oct 27 '22 19:10

Paul Cantrell