在Swift Package中如何使用依赖项

3
我使用Xcode创建了一个新的Package并引入了一个依赖项,但当我尝试使用它时,出现了错误。

Swift Package Error

如何在软件包源中使用依赖项?在普通的项目中,我可以轻松地导入和使用AgileDB。
以下是该软件包:

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

import PackageDescription

let package = Package(
    name: "DBCore",
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "DBCore",
            targets: ["DBCore"]),
    ],

    dependencies: [
          .package(url: "https://github.com/AaronBratcher/AgileDB", from: "6.4.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: "DBCore",
            dependencies: []),
        .testTarget(
            name: "DBCoreTests",
            dependencies: ["DBCore"]),
    ]
)

也许目标中需要依赖于 AgileDB 包?我尝试复制它,但无法识别。

AgileDB 内部检查文件 Package。其中会有类似于 name: "X" 的内容。尝试在你的导入中使用它。 - Suyash Medhavi
@SuyashMedhavi 这个名称是AgileDB,可以在这里找到:https://github.com/AaronBratcher/AgileDB - Aaron Bratcher
我已经添加了一个详细的答案,解释如何添加一个库。请检查是否可能在这里错过了一步或所有必需的条目都按照要求存在。 - Suyash Medhavi
2个回答

1

找到了我的答案。

在目标依赖项中,需要将包名称作为字符串包含:

        // 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: "DBCore",
            dependencies: [
                    "AgileDB"
                ]),

        .testTarget(
            name: "DBCoreTests",
            dependencies: ["DBCore"]),
    ]

-3

逐步指南:

enter image description here

  1. 打开项目设置
  2. 点击“包依赖项”
  3. 点击“加号”图标并在打开的窗口中添加您的包
  4. 您添加的包应出现在列表中

enter image description here

  1. 包也应该出现在左侧栏的“包依赖项”下

enter image description here

  • 点击您的目标
  • 导航到“常规”选项卡和其中的“框架和库”部分
  • 单击“加号”图标并在打开的窗口中选择/添加您的软件包
  • 您添加的软件包应该出现在列表中
  • enter image description here

    1. 应该自动将包添加到“链接二进制文件和库”部分下的“构建阶段”选项卡中。如果没有,请通过单击“加号”图标来添加它。

    此时,您应该能够在此目标下的任何文件中导入和使用该包。


    我的"项目"是一个Swift Package。请参见我问题中的图片。你指示中提到的项目或目标都没有。虽然你展示的是好的,但我已经在别处有这个工作了。我建议删除这个答案。 - Aaron Bratcher

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接