Swift Package Manager - 类型 'Bundle' 没有成员“module”错误

14

在实现框架的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"]
        )
    ]
)

尝试访问捆绑包中的一项资源时,我遇到了 Type 'Bundle' has no member "module" 错误:

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

项目在 GitHub 上,这里


.process("Resources")在哪里? - Mojtaba Hosseini
此外,你必须使用 swift-tools-version:5.3 来访问资源。 - Mojtaba Hosseini
抱歉,我复制粘贴了错误的Package.swift文件 - 现在已经在问题中修正。但不幸的是,仍然出现错误。 - koen
7个回答

11

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

除去测试文件中的所有错误,唯一剩下的问题是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打开它或将其导入为软件包(而不是将其转换为项目)!

顺便提一下,这是生成的文件,所以当您在与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")
    }()
}

我已经使用正确的Package.swift文件更新了我的问题,但仍然出现错误。无论是否使用?都是如此。还有其他想法可能出了什么问题吗?难道是因为我将清单文件添加到具有.xcodeproj文件的现有框架中吗? - koen
不太确定我是否理解了,如果没有 xcodeproj 文件,我该如何进行编译等操作? - koen
仍然困惑,如果没有xcodeproj文件,我该如何打开项目以便构建? - koen
2
双击 package.swift 文件。 - Mojtaba Hosseini
2
我直接使用Package.swift,但仍然出现了这样的错误。 - JRafaelM
显示剩余6条评论

2
如果您按照此视频上的说明操作,您将会发现需要定义如何在包中包含非明确目的的文件。在这种情况下,您的包清单应该类似于:
// 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工具,并且您需要为相应的目标声明资源。资源上的复制操作将在它们添加到编译的捆绑包中时使它们未经处理。

0

我曾经遇到过同样的问题(当我创建了一个全新的包),我的解决方法是在我的包内添加了一个新的视图文件。这样错误就消失了。


0

我曾经遇到过同样的问题,但是我用了老套路——“你试过关掉再打开吗?”。不过说真的,我只是在我的代码中使用了Bundle.module(也可以只是一行代码,比如print(Bundle.module)),然后完全关闭Xcode,通过Package.swift文件重新打开包,这样就解决了。


0
请检查您的资源文件夹路径。它应该保存在Sources/PackageNameFolder/{Resources}中。在此输入图像描述

0
在.xcproject上遇到了同样的问题,这个问题是由于添加了一个包含.process("Resources/")的本地包所引起的。简单地重新启动Xcode就解决了这个问题。

0

我为了在我的 Swift Package 之外的应用程序或另一个项目中访问 Bundle.module 对象而做的事情是在包内创建一个公开可访问的变量,例如:

public static var myModuleNameBundle: Bundle { Bundle.module }

myModuleNameBundle中的myModuleName替换为您的Swift Package名称


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接