Swift 包管理器 - 类型“Bundle"没有成员“模块"错误 [英] Swift Package Manager - Type 'Bundle' has no member “module” error

查看:39
本文介绍了Swift 包管理器 - 类型“Bundle"没有成员“模块"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在为框架实现 SPM,但遇到了 Type 'Bundle' has no member module" 错误.

Working on implementing SPM for a framework, and got stuck on the Type 'Bundle' has no member "module" error.

我最近看到了另外两篇关于这个的帖子这里这里,但是按照所有步骤,它是仍然不适合我,没有生成 resource_bundle_accessor 文件.

I have seen two other recent posts about this here and here, but following all the steps, it is still not working for me, no resource_bundle_accessor file is generated.

我在此处询问了我的 Package.swift 文件,并且已经得到回答和解决.为了完整起见,这里是文件:

I asked about my Package.swift file here, and that has been answered and resolved. For completeness here's the file:

// 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" 错误:

I get the Type 'Bundle' has no member "module" error when trying to access one of the resources in the bundle:

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

该项目位于 GitHub 这里

The project is on GitHub here

推荐答案

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

Besides all errors in the test file, the only issue remains is that module is not an optional. To fix that, just change this:

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

到:

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


更新:

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


Update:

If you use .xcodeproj file, you will continue seeing this error. You should consider opening it with the package.swift or import it as a package (instead of converting it to a project)!

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

by the way, here is the generated file, so you can add it as a development asset when you are working with the 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")
    }()
}

这篇关于Swift 包管理器 - 类型“Bundle"没有成员“模块"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆