Qua*_*eur 5 code-structure swift swift-package-manager swiftpm
我有一个本地 swift 包Foo,它提供了两个不同的库Foo和FooB。现在我想在另一个本地包Bar 中使用它们。我只能通过路径声明来获取整个包。有没有办法命名/指定应使用哪个库?我想在我的Bar包中使用FooB。
let package = Package(
name: "Foo",
products: [
.library(name: "Foo", targets: ["Foo"]),
.library(name: "FooB", targets: ["FooB"])
]
...)
Run Code Online (Sandbox Code Playgroud)
let package = Package(
name: "Bar",
dependencies: [
.package(path: "Foo"),
.package(path: "FooB") // this one does not work
],
...)
Run Code Online (Sandbox Code Playgroud)
// inside package Bar
import Foo
import FooB // this is throwing "no such module 'FooB'"
Run Code Online (Sandbox Code Playgroud)
这是一个具体的例子:
\n正在播放Foo, IGListKit Package.swift
摘录:
\nproducts: [\n .library(name: "IGListDiffKit",\n type: .static,\n targets: ["IGListDiffKit"]),\n .library(name: "IGListKit",\n type: .static,\n targets: ["IGListKit"]),\nRun Code Online (Sandbox Code Playgroud)\n\xe2\x80\xa6
\ntargets: [\n .target(\n name: "IGListDiffKit",\n path: "spm/Sources/IGListDiffKit"\n ),\n .target(\n name: "IGListKit",\n dependencies: ["IGListDiffKit"],\n path: "spm/Sources/IGListKit"\n ),\nRun Code Online (Sandbox Code Playgroud)\n就 的整体而言Bar:
import PackageDescription\n\nlet package = Package(\n name: "HistoryList",\n platforms: [.iOS(.v13)],\n products: [\n .library(\n name: "HistoryList",\n targets: ["HistoryList"]),\n ],\n dependencies: [\n .package(name: "IGList", url: "https://github.com/Instagram/IGListKit", from: "4.0.0"),\n ],\n targets: [\n .target(\n name: "HistoryList",\n dependencies: [\n .product(name: "IGListDiffKit", package: "IGList"),\n .product(name: "IGListKit", package: "IGList")\n ]\n ),\n ]\n)\nRun Code Online (Sandbox Code Playgroud)\n导入后, Package DependenciesFoo部分中的Package.swift仅说明:
let package = Package(\n name: "IGListKit",\n products: [\n .library(name: "IGListKit", targets: ["IGListKit"])\n ],\n targets: [\n .target(\n name: "IGListKit",\n path: "Source"\n )\n ]\n)\nRun Code Online (Sandbox Code Playgroud)\nXcode / SPM 似乎正在修剪导入的包,以仅包含该包中的三个库之一。(最初,第二个库的代码丢失了,我的语法导致它被拉出,但第一个库没有看到。它们现在都在那里,但只看到一个。)
\n我无法确定一种语法来使其同时关闭两者。
\nproduct \'IGListDiffKit\' required by package \'ios-malauzai-history-list\' target \'HistoryList\' not found in package \'IGList\'.\nRun Code Online (Sandbox Code Playgroud)\nXcode 15.0 测试版 (15A5160n)
\npackage(name:url:_:)(以及name的属性Package.Dependency)已被弃用,但您需要使用包依赖项的名称在特定目标依赖项中使用。
这让我相信应该使用一种更新的语法。
\n(有比 IGListKit 更好的解决方案,但这是在遗留代码中。目前,4.0.0标签和存储库的 HEAD 之间有 191 次提交;距离发布已经 3 年多了。)
本例中的问题是,最新的标签(截至 2023 年 6 月)是标签4.0.0,文件的版本Package.swift实际上是我收到的缩写版本。
当我更新时一切正常,并简化了描述以查看添加支持的提交。(本来可以使用main分支,但这会不断变化。)
\xe2\x80\xa6\n dependencies: [\n .package(url: "https://github.com/Instagram/IGListKit", revision: "c9e045c9425a160871a7915260d07a6c273b548d")\n ],\n targets: [\n .target(\n name: "HistoryList",\n dependencies: [\n .product(name: "IGListKit", package: "IGListKit"),\n .product(name: "IGListDiffKit", package: "IGListKit")\n ]\n ),\n\xe2\x80\xa6\nRun Code Online (Sandbox Code Playgroud)\n