简体   繁体   English

使用 Swift Package Manager 时如何访问捆绑包

[英]How to access the bundle when using Swift Package Manager

I have a framework that supports CocaPods .我有一个支持CocaPods的框架。 I have now added support for Swift Package Manager but experiencing a crash when trying to access the framework's bundle.我现在添加了对Swift Package Manager的支持,但在尝试访问框架的捆绑包时遇到了崩溃。

    static func debuggerBundle(from target: AnyClass) -> Bundle {
        let podBundle = Bundle(for:  target)
        guard let bundleURL = podBundle.url(forResource: "Harlow", withExtension: "bundle"),
        let bundle = Bundle(url: bundleURL) else { fatalError("Must have a local bundle") }
        return bundle
    }

When installing with CocoaPods , the .ipa contains a /Frameworks folder with all the frameworks.使用CocoaPods安装时, .ipa包含一个包含所有框架的/Frameworks文件夹。 But when running with SPM , the frameworks do not exist.但是当使用SPM运行时,框架不存在。

Using CocoaPods使用 CocoaPods

使用 CocoaPods

Using SPM使用 SPM

使用 SPM

How do we access the framework's bundle with SPM?我们如何使用 SPM 访问框架的捆绑包?

https://github.com/stanwood/Harlow/ https://github.com/stanwood/Harlow/

As mentioned in the comments, SPM package version 5.3 is the minimum for supporting bundle resources.正如评论中提到的,SPM package 版本 5.3 是支持捆绑资源的最低要求。 If you need to maintain support for 5.1, you can add a second package file that includes support for 5.3: Package@swift-5.3.swift .如果您需要保持对 5.1 的支持,您可以添加第二个 package 文件,其中包含对 5.3 的支持: Package@swift-5.3.swift

Using your Harlow repo as a base, the new Package file will look like this:以您的 Harlow 存储库为基础,新的 Package 文件将如下所示:

// 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: "Harlow",
    platforms: [
        .iOS(.v10)
    ],
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "Harlow",
            targets: ["Harlow"]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        .package(url: "https://github.com/shu223/Pulsator.git", .upToNextMajor(from: "0.6.3")),
        .package(url: "https://github.com/schmidyy/Loaf.git", .upToNextMajor(from: "0.7.0")),
        .package(url: "https://github.com/stanwood/SourceModel_iOS.git", .upToNextMajor(from: "1.3.3"))
    ],
    targets: [
        .target(
            name: "Harlow",
            dependencies: ["Pulsator", "SourceModel", "Loaf"],
            resources: [.copy("Resources")]
        ),
        .testTarget(
            name: "HarlowTests",
            dependencies: ["Harlow"]),
    ],
    swiftLanguageVersions: [.v5]
)

This will make the Bundle.module reference become available.这将使Bundle.module引用变得可用。 You could provide a static reference to this instance available through your Bundle extension like this:您可以通过您的Bundle扩展提供一个 static 对这个实例的引用,如下所示:

extension Bundle {
    public static var harlow: Bundle = .module
}

With that in place, you should be able to use the harlow bundle reference to load your DefaultSettings.plist .有了这些,您应该能够使用harlow捆绑包引用来加载您的DefaultSettings.plist ( Bundle.harlow.url(forResource:withExtension:) ) ( Bundle.harlow.url(forResource:withExtension:) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM