如何在自己的Swift包中使用其他Swift包?

ghg1uchk  于 6个月前  发布在  Swift
关注(0)|答案(2)|浏览(90)

此问题在此处已有答案

Swift Package Manager, How to add a package as development dependency?(3个答案)
一年前关闭。
我目前正在开发一个Swift包,我想在其中使用其他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"))
],

字符串
这是我的完整包。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"]),
    ]
)


然后这些软件包出现在项目导航器的“软件包目录”下。但是当我尝试使用“导入Alamofire”时,我得到一个错误,说“没有这样的模块'Alamofire'”。如何在我自己的软件包中正确使用这些软件包?

yshpjwxd

yshpjwxd1#

如前所述,您必须将依赖项“Alamofire”和“Sodium”设置为目标。例如:

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

字符串
我建议也给上面定义的依赖包给予名称:

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"))
    ]

pwuypxnk

pwuypxnk2#

您需要将它们添加到目标的依赖项部分。像这样:

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: ["Alamofire"]),
    .testTarget(
        name: "SpaceCryptographyTests",
        dependencies: ["SpaceCryptography", "Alamofire"]),
]

字符串

相关问题