Swift 包管理器 - 类型 'Bundle' 没有成员“模块”错误

Swift Package Manager - Type 'Bundle' has no member “module” error

正在为框架实施 SPM,但遇到 Type 'Bundle' has no member “module” 错误。

我最近看到了另外两篇关于这个 and here 的帖子,但是按照所有步骤,它仍然对我不起作用,没有生成 resource_bundle_accessor 文件。

我询问了我的 Package.swift 文件 ,该问题已得到答复和解决。为了完整起见,这里是文件:

// 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: "BioSwift",
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "BioSwift",
            targets: ["BioSwift"]
        )
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages this package depends on.
        .target(
            name: "BioSwift",
            dependencies: [],
            resources: [
                  .process("Resources")
                ]
        ),
        .testTarget(
            name: "BioSwiftTests",
            dependencies: ["BioSwift"]
        )
    ]
)

我在尝试访问包中的资源之一时收到 Type 'Bundle' has no member “module” 错误:

public let unimodURL = Bundle.module?.url(forResource: "unimod", withExtension: "xml")

项目在GitHubhere

如果您按照此 video 上的说明进行操作,您会发现您需要定义希望如何将 non-clear 目的文件包含在包中。在这种情况下,你的包清单应该是这样的:

// swift-tools-version:5.3

import PackageDescription

let package = Package(
    name: "BioSwift",
    products: [
        // Products define the executables and libraries produced by a package, and make them visible to other packages.
        .library(
            name: "BioSwift",
            targets: ["BioSwift"]),
    ],
    dependencies: [],
    targets: [
        .target(
            name: "BioSwift",
            dependencies: [],
            resources: [
              .copy("Resources")
            ]),
        .testTarget(
            name: "BioSwiftTests",
            dependencies: ["BioSwift"]),
    ]
)

请注意使用Swift 5.3 工具,您需要为各自的目标声明资源。将资源添加到已编译的包中时,对资源的复制操作将使它们处于未处理状态。

除了测试文件中的所有错误外,唯一的问题是 module 不是 optional。要解决这个问题,只需更改此:

public let unimodURL = Bundle.module?.url(forResource: "unimod", withExtension: "XML")

至:

public let unimodURL = Bundle.module.url(forResource: "unimod", withExtension: "xml")

更新:

如果您使用 .xcodeproj 文件,您将继续看到此错误。您应该考虑使用 package.swift 打开它或将其作为包导入(而不是将其转换为项目)!

顺便说一句,这是生成的文件,因此您可以在使用 xcode 项目时将其添加为开发资产:

import class Foundation.Bundle

private class BundleFinder {}

extension Foundation.Bundle {
    /// Returns the resource bundle associated with the current Swift module.
    static var module: Bundle = {
        let bundleName = "BioSwift_BioSwift"

        let candidates = [
            // Bundle should be present here when the package is linked into an App.
            Bundle.main.resourceURL,

            // Bundle should be present here when the package is linked into a framework.
            Bundle(for: BundleFinder.self).resourceURL,

            // For command-line tools.
            Bundle.main.bundleURL,
        ]

        for candidate in candidates {
            let bundlePath = candidate?.appendingPathComponent(bundleName + ".bundle")
            if let bundle = bundlePath.flatMap(Bundle.init(url:)) {
                return bundle
            }
        }
        fatalError("unable to find bundle named BioSwift_BioSwift")
    }()
}