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

koe*_*oen 5 xcode swift swift-package-manager

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

我最近在这里这里看到了另外两篇关于此的帖子,但是按照所有步骤操作,它仍然对我不起作用,没有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"]
        )
    ]
)
Run Code Online (Sandbox Code Playgroud)

我得到的 Type 'Bundle' has no member “module”努力被捆绑的资源的访问之一,当错误:

public let unimodURL = Bundle.module?.url(forResource: "unimod", withExtension: "xml")
Run Code Online (Sandbox Code Playgroud)

该项目是在GitHub上这里

Moj*_*ini 6

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

public let unimodURL = Bundle.module?.url(forResource: "unimod", withExtension: "XML")
Run Code Online (Sandbox Code Playgroud)

到:

public let unimodURL = Bundle.module.url(forResource: "unimod", withExtension: "xml")
Run Code Online (Sandbox Code Playgroud)

更新:

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

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

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")
    }()
}
Run Code Online (Sandbox Code Playgroud)

  • 双击package.swift (2认同)
  • 我直接使用 Package.swift ,但仍然收到这样的错误 (2认同)