swiftxcodeswift-package-managerswift-package

How to use other swift packages in an own swift package?


I'm currently developing a Swift Package in which I would like to use other Swift Packages. To do so I added the packages I want to use to the dependencies in my Package.swift

dependencies: [
    // Dependencies declare other packages that this package depends on.
    // .package(url: /* package url */, from: "1.0.0"),
    .package(url: "https://github.com/jedisct1/swift-sodium.git", .upToNextMajor(from: "0.9.1")),
    .package(url: "https://github.com/Alamofire/Alamofire.git", .upToNextMajor(from: "5.5.0"))
],

This is my full Package.swift:

// swift-tools-version:5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "SpaceCryptography",
    platforms: [
        .iOS(.v10),
        .watchOS(.v3)
    ],
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "SpaceCryptography",
            targets: ["SpaceCryptography"]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
        .package(url: "https://github.com/jedisct1/swift-sodium.git", .upToNextMajor(from: "0.9.1")),
        .package(url: "https://github.com/Alamofire/Alamofire.git", .upToNextMajor(from: "5.5.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 this package depends on.
        .target(
            name: "SpaceCryptography",
            dependencies: []),
        .testTarget(
            name: "SpaceCryptographyTests",
            dependencies: ["SpaceCryptography"]),
    ]
)

The packages then appear under "Package Dependencies" in the Project Navigator. But when I try to use "import Alamofire" I get an error saying "No such module 'Alamofire'". How to properly use these packages in my own package?


Solution

  • As previous mentioned, you have to set the dependencies "Alamofire" and "Sodium" to the target. For example:

    .target(
       name: "SpaceCryptography",
       dependencies: ["Alamofire", "Sodium"]),
    

    I recommend to also give names to the dependencies packages defined above:

    dependencies: [
            // Dependencies declare other packages that this package depends on.
            // .package(url: /* package url */, from: "1.0.0"),
            .package(name: "Sodium", url: "https://github.com/jedisct1/swift-sodium.git", .upToNextMajor(from: "0.9.1")),
            .package(name: "Alamofire" url: "https://github.com/Alamofire/Alamofire.git", .upToNextMajor(from: "5.5.0"))
        ]