Ios 在本地SPM包中导入本地SPM库

Ios 在本地SPM包中导入本地SPM库,ios,swift,xcode,dependencies,swift-package-manager,Ios,Swift,Xcode,Dependencies,Swift Package Manager,我有一个包含2个库的本地SPM包,我想在另一个本地SPM包中导入其中一个库: 包含库的文件: let package = Package( name: "LocationService", platforms: [.iOS(.v13)], products: [ .library( name: "LocationService", type: .dynamic,

我有一个包含2个库的本地SPM包,我想在另一个本地SPM包中导入其中一个库: 包含库的文件:

let package = Package(
    name: "LocationService",
    platforms: [.iOS(.v13)],
    products: [
        .library(
            name: "LocationService",
            type: .dynamic,
            targets: ["LocationService"]),
        .library(
          name: "LocationLiveClient",
          type: .dynamic,
          targets: ["LocationLiveClient"]),
    ],
    targets: [
        .target(
            name: "LocationService",
            dependencies: []),
        .target(
            name: "LocationLiveClient",
            dependencies: ["LocationService"],
            path: "Sources/LocationLiveClient"),
    ]
)
let package = Package(
    name: "HomePage",
    products: [
        .library(
            name: "HomePage",
            type: .dynamic,
            targets: ["HomePage"])
    ],
    dependencies: [
      .package(path: "../RouterService"),
      .package(path: "../LocationService/Sources/LocationLiveClient"),
    ],
    targets: [
        .target(
            name: "HomePage",
            dependencies: ["RouterService", "LocationLiveClient"])
    ]
)
文件导入库:

let package = Package(
    name: "LocationService",
    platforms: [.iOS(.v13)],
    products: [
        .library(
            name: "LocationService",
            type: .dynamic,
            targets: ["LocationService"]),
        .library(
          name: "LocationLiveClient",
          type: .dynamic,
          targets: ["LocationLiveClient"]),
    ],
    targets: [
        .target(
            name: "LocationService",
            dependencies: []),
        .target(
            name: "LocationLiveClient",
            dependencies: ["LocationService"],
            path: "Sources/LocationLiveClient"),
    ]
)
let package = Package(
    name: "HomePage",
    products: [
        .library(
            name: "HomePage",
            type: .dynamic,
            targets: ["HomePage"])
    ],
    dependencies: [
      .package(path: "../RouterService"),
      .package(path: "../LocationService/Sources/LocationLiveClient"),
    ],
    targets: [
        .target(
            name: "HomePage",
            dependencies: ["RouterService", "LocationLiveClient"])
    ]
)

这里有几个问题需要解决

(如果您的设计要求使用“动态”链接,则此方法可能不适用于您。)

  • 类型:。动态
除非您绝对需要保证如何实现库链接,否则建议您将其保留为默认值
nil
(只需删除该行)。这允许swift包管理器确定如何最好地链接库(默认为“静态”)

  • .package(路径:../LocationService/Sources/LocationLiveClient”),
LocationLiveClient
LocationService
包的产品和目标。在这里的依赖项中,应该作为一个整体引用包。因此,将其更改为
.package(路径:“../LocationService”),

  • 依赖项:[“RouterService”、“LocationLiveClient”])
一旦更改依赖于整个位置服务包,编译器就需要一些额外的信息。您可以更新目标依赖项,以专门使用
LocationService
包中的
LocationLiveClient
库:
.product(名称:“LocationLiveClient”,包:“LocationService”)

这些更改到位后,您将得到如下包定义:

let package=package(
名称:“主页”,
产品:[
.图书馆(
名称:“主页”,
目标:[“主页]),
],
依赖项:[
.package(路径:../RouterService”),
.package(路径:“../LocationService”),
],
目标:[
.目标(
名称:“主页”,
依赖项:[
“RouterService”,
.产品(名称:“LocationLiveClient”,软件包:“LocationService”)
]
),
]
)
然后,您应该能够按预期导入LocationLiveClient


旁注:假设您的“LocationService”包具有以下文件夹结构,那么您可以安全地从
LocationLiveClient
目标定义中删除
path:“Sources/LocationLiveClient”

LocationService
-> Sources
  -> LocationService
  -> LocationLiveClient
谢谢:)成功了,我错过了一个事实,我可以添加整个产品作为一个依赖项。