[英]How to import a package's dependency when using Swift Package Manager?
[英]How to access the bundle when using Swift Package Manager
我有一个支持CocaPods
的框架。 我现在添加了对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
}
使用CocoaPods
安装时, .ipa
包含一个包含所有框架的/Frameworks
文件夹。 但是当使用SPM运行时,框架不存在。
使用 CocoaPods
使用 SPM
我们如何使用 SPM 访问框架的捆绑包?
正如评论中提到的,SPM package 版本 5.3 是支持捆绑资源的最低要求。 如果您需要保持对 5.1 的支持,您可以添加第二个 package 文件,其中包含对 5.3 的支持: Package@swift-5.3.swift
。
以您的 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]
)
这将使Bundle.module
引用变得可用。 您可以通过您的Bundle
扩展提供一个 static 对这个实例的引用,如下所示:
extension Bundle {
public static var harlow: Bundle = .module
}
有了这些,您应该能够使用harlow
捆绑包引用来加载您的DefaultSettings.plist
。 ( Bundle.harlow.url(forResource:withExtension:)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.