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

ghg1uchk  于 2024-01-05  发布在  Swift
关注(0)|答案(2)|浏览(197)

此问题在此处已有答案

Swift Package Manager, How to add a package as development dependency?(3个答案)
一年前关闭。
我目前正在开发一个Swift包,我想在其中使用其他Swift包。为此,我将我想使用的包添加到我的包中的依赖项中。

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

字符串
这是我的完整包。swift:

  1. // swift-tools-version:5.5
  2. // The swift-tools-version declares the minimum version of Swift required to build this package.
  3. import PackageDescription
  4. let package = Package(
  5. name: "SpaceCryptography",
  6. platforms: [
  7. .iOS(.v10),
  8. .watchOS(.v3)
  9. ],
  10. products: [
  11. // Products define the executables and libraries a package produces, and make them visible to other packages.
  12. .library(
  13. name: "SpaceCryptography",
  14. targets: ["SpaceCryptography"]),
  15. ],
  16. dependencies: [
  17. // Dependencies declare other packages that this package depends on.
  18. // .package(url: /* package url */, from: "1.0.0"),
  19. .package(url: "https://github.com/jedisct1/swift-sodium.git", .upToNextMajor(from: "0.9.1")),
  20. .package(url: "https://github.com/Alamofire/Alamofire.git", .upToNextMajor(from: "5.5.0"))
  21. ],
  22. targets: [
  23. // Targets are the basic building blocks of a package. A target can define a module or a test suite.
  24. // Targets can depend on other targets in this package, and on products in packages this package depends on.
  25. .target(
  26. name: "SpaceCryptography",
  27. dependencies: []),
  28. .testTarget(
  29. name: "SpaceCryptographyTests",
  30. dependencies: ["SpaceCryptography"]),
  31. ]
  32. )


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

yshpjwxd

yshpjwxd1#

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

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

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

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

展开查看全部
pwuypxnk

pwuypxnk2#

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

  1. targets: [
  2. // Targets are the basic building blocks of a package. A target can define a module or a test suite.
  3. // Targets can depend on other targets in this package, and on products in packages this package depends on.
  4. .target(
  5. name: "SpaceCryptography",
  6. dependencies: ["Alamofire"]),
  7. .testTarget(
  8. name: "SpaceCryptographyTests",
  9. dependencies: ["SpaceCryptography", "Alamofire"]),
  10. ]

字符串

相关问题