Swift Package Manger is well supported in Xcode 11.

So how do you make an existing Framewrok support Swift Package Manager? This article uses my own open source project, HSStockChart, as an example.

Use swift Package init

First, in the project directory (including the.xcodeProj file directory), do the following: 👇

swift package init --type library
Copy the code

You should see output like this:

Executing this directive generates the directories and related files required by the Swift Package Manger in this directory.

Modify package. swift and the file directory as needed

We can see that this command generates the Sources and Tests directories for us, along with some files. However, the code of my original project was not stored in this directory, and I didn’t want to move the code to these default directories, and I didn’t configure the Target of the Tests, so I just deleted Sources and Tests and specified my code path in package. swift.

Here is my modified package.swift:

import PackageDescription

let package = Package(
    name: "HSStockChart",
    products: [
        // Products define the executables and libraries produced by a package, and make them visible to other packages.
        .library(
            name: "HSStockChart",
            targets: ["HSStockChart"]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        / / package (url: package / * * / url, from: "1.0.0"),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages which this package depends on.
        .target(
            name: "HSStockChart",
            dependencies: [],
            path: "HSStockChart")])Copy the code

Note here that I added:

path: "HSStockChart"
Copy the code

“HSStockChart” is my source path. I also removed the testTarget content

The test results

We can create a new project and introduce HSStockChart to test it.

  • First select Project –> Swift Packages –> click “+”

  • Then in the window that pops up, enter the Github address for your project

  • Note that Branch is selected as master in this step because we haven’t tagged the new version yet

  • Finally, we can see HSStockChart introduced in the Project Navigator