swift - 如何在 Swift Package Manager 中添加本地库作为依赖项

标签 swift

如何在 Swift 包管理器中添加本地库(*.a 文件)作为依赖项?

我尝试在我的 Package.swift 中添加:

dependencies: [
    // Dependencies declare other packages that this package depends on.,
    .package(url: "file://../otherdirectory/x86_64-apple-macosx/debug/libTest.a")
],

但是当我运行'swift build'时出现这个错误

Package.swift:17:10: error: type of expression is ambiguous without more context

最佳答案

首先:package 依赖只能链接到其他包!

Swift 5.3 开始,使用 binaryTarget 是可能的,但您应该使用几个所需的架构(arm64、x86_64)构建静态库,然后使用下一个命令从它们创建 XCFramework:

xcodebuild -create-xcframework \
    -library <path> [-headers <path>] \
    [-library <path> [-headers <path>]...] \
    -output <path>

例如:

xcodebuild -create-xcframework \
    -library build/simulators/libMyStaticLib.a \
    -library build/devices/libMyStaticLib.a \
    -output build/MyStaticLib.xcframework

然后你可以在你的包中创建新的二进制目标依赖:

let package = Package(
   name: "MyPackage",
   ...
   targets: [
      .target(
         name: "MyPackage",
         dependencies: ["MyStaticLib"]
      ),
      .binaryTarget(name: "MyStaticLib", path: "path/MyStaticLib.xcframework"),
      ...
   ]

注意:xcframework的路径从项目的根目录开始(同Package.swift)。

关于swift - 如何在 Swift Package Manager 中添加本地库作为依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63607450/

相关文章:

ios - 以编程方式在 UITableViewCell 中制作 UIButton 时出现问题

ios - ARC 的 viewController 中的变量

ios - 将swift程序分成多个文件

swift - 将可选内容展开为实现接口(interface)的条件对象

ios - 在 UIView 之间委派 - 将接收者的委托(delegate)放在哪里?

ios - 快速兼容性

arrays - 迭代一系列字典以获得最高的 gpa

arrays - 如何在标签中显示数组?

swift - RxSwift + canMoveRowAt

swift - 为什么我的 SemVer NSRegularExpression 不能运行?