[SOLVED] Add Swift Package to a custom framework

Issue

Pretty new to creating frameworks with SPM dependencies. So I made a new framework project, added some of my classes/files as well as a SPM dependency (CocoaLumberjack logger). Framework compiles fine.

When I look for my framework product that I’m planning on embedding into some other project I see that it is in my Products folder. Alongside with it I see CocoaLumberjack module. Inside of my framework there is not much beside the exec file.

When I try to embed my framework into some other projects. Nothing compiles because it says that CocoaLumberjack module is missing.

Does anyone know how to fix this? Am I missing an important step or soemthing?

Solution

Well, there are numerous isses you could have faced during importing framework itself. It also depends if you use framework as binary or source code. I assume you were using source code approach as you are the creator of framework. You can however check all approaches here: in this SO question . Lets look at all the steps you need to implement in order to successfully use framework with SPM dependencies in your swift project.

  1. create SPM properly and also link all additional SPM dependencies tutorial here. Make sure all your classes, structs etc. and their coresponding initializer has correct access level property. If you plan to use them outside of the package, use public initializers..
    2)Once you created you SPM package, link it to framework. For the sake of this answer I created testFramework and linked one of my custom SPM package called VodApiPackage . This package also contains dependency to another BaseTvApiServicePackage.

screenshot1

I also added TestPrinter file containing simple function for creating error declared in my SPM package. This function servers only for checking that everything is working properly and will be user later. It is also declared public.

import Foundation
import VodApiPackage

public struct TestPrinter {
public init () {}

public func makeTest() {
    let x = VodApiError.customErr(msg: "testMsg")
    print(x.localizedDescription)
}

}

  1. Open your project and make link to framework, you can also check this nice tutorial. The most important step from tutorial is step 5 and 6. Where you drag .xcproj into your project and link libraries and framework

  2. make sure your library and SPM dependencies are correctly linked in your project. Check sample project below.

screenshot1

  1. Build and test using your framework and its packages:

    import UIKit
    import testFramework
    
    class ViewController: UIViewController {
    
    
     override func viewDidLoad() {
      super.viewDidLoad()
      testmodel()
    
     TestPrinter().makeTest()
     }
    }
    

Answered By – Mr.SwiftOak

Answer Checked By – Mary Flores (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.