【问题标题】:How to use Swift Package Manager's binaryTarget?如何使用 Swift 包管理器 binaryTarget?
【发布时间】:2020-07-08 16:04:20
【问题描述】:

我正在尝试使用 Swift 包管理器的 binaryTarget 来包含 https://github.com/stripe/stripe-ios/releases/tag/v19.3.0 此处提供的 Stripe xcframework。包管理器没有抱怨,让我链接到它,但我无法导入它。我已经制作了一个示例 repo 在这里展示它https://github.com/martyu/StripePackageTest。我错过了什么吗?

【问题讨论】:

    标签: swift swift-package-manager


    【解决方案1】:

    首先,你的例子是不可测试的,因为你忘记提供版本标签,所以这不是一个真正的包。

    其次,更重要的是,我认为您对包如何作为二进制目标存在误解。您似乎认为您的 Swift 包可以包含看到 XCFramework 的代码。 (这就是为什么您要在包的源代码中的框架模块中尝试import。)那是错误的。导入框架模块的是 app。包只是分发框架的一种方式。

    换句话说,您可以编写源代码包或框架承载包。一个包不能两者兼有。

    当然你可以写一个源代码包,依赖一个框架包。

    【讨论】:

    • 我根本不想使用框架,但 Stripe 存储库不支持 SPM。所以我以为我被他们发布的 .xcframework 卡住了?我没有标记它,因为我希望人们下载并尝试构建它,而不是从另一个应用程序 shrug 导入它。但它不会在那里使用 import 语句构建。
    • 也许我对binaryTarget 是什么感到困惑。我将依赖 binaryTarget 等同于将框架拖到应用程序的 xcodeproj 中。
    • @matt 我对这个答案感到困惑,并且在文档顶部附近看到的冲突说明:developer.apple.com/documentation/swift_packages/…“Swift 包可以包含源文件和二进制文件的混合。这个用途case 对于包含封装闭源二进制文件的源代码的包很常见。"
    • @IgorGanapolsky 提供 =“创建和推送”。 version = "版本字符串"。标签 = “标签” (Git)
    【解决方案2】:

    首先,您不需要 需要一个版本标签才能使其成为“真正的包”。 You can specify package dependencies via commit SHA and branch as well。您也可以通过file:// 在 xcode 中添加本地包 repos。请注意,这与本地开发覆盖不同。

    我对@9​​87654323@ 不太满意,但通过在 Xcode 中创建应用程序并将包添加到其中,我确实让它正常工作。我认为这就是@matt 的意思。您需要将它导入到项目(xcode 或其他 SP)中,然后 xcode 会在 ~~builds~~ 感觉像它时组装所有依赖项。

    这是我使用的修改后的 Package.swift。我将名称更改为 Example(因为这可能是您正在构建的依赖于 Stripe 的 SDK)。如果您希望将“Stripe”嵌入到其框架中,您可以在示例库的目标中包含它。否则客户端应用也只需要导入它(在 Xcode 中添加它时通过复选框,或在另一个 Package.swift 中通过 dependencies)。

    // swift-tools-version:5.3
    // The swift-tools-version declares the minimum version of Swift required to build this package.
    
    import PackageDescription
    
    let package = Package(
        name: "Example",
        platforms: [
            .iOS(.v11)
        ],
        products: [
            .library(
              name: "Example",
              type: .dynamic,
              targets: ["Example"]),
            .library(
                name: "Stripe",
                targets: ["Stripe"])
        ],
        dependencies: [],
        targets: [
            // I thought this was defining the Stripe binaryTarget...
            .binaryTarget(name: "Stripe",
                          url: "https://github.com/stripe/stripe-ios/releases/download/v19.3.0/Stripe.xcframework.zip",
                          checksum: "fe459dd443beee5140018388fd6933e09b8787d5b473ec9c2234d75ff0d968bd"),
            // ... and then linking it to the Example project here via "dependencies" ...
            .target(name: "Example", dependencies: ["Stripe"], path: "Sources")
            // ... so when I'm in "Example" files, I thought I'd be able to import "Stripe" to them
        ]
    )
    

    【讨论】:

    • 如何配置一个 binaryTarget,它是 xcframework zip 文件,但在一个私人仓库中。我尝试了 SSH,但你能举例说明 Package.swift 的样子吗?
    • @Max 我的猜测是你要么有一个 https 或一个 git 协议的 url,就像公共回购一样。您只需要确保使用您的 SPM 的人在添加您的 swift 包时可以访问私有仓库。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 2018-09-23
    相关资源
    最近更新 更多