Generating resource_bundle_accessor, Type 'Bundle' has no member 'module'

萝らか妹 提交于 2020-12-22 08:12:46

问题


Some times Xcode can not determine the module parameter in the Bundle.

Type 'Bundle' has no member 'module'

My investigations show that SPM generates an extension on the module (some times) for this property automatically in a file called resource_bundle_accessor like:

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 = "ABUIKit_ABStyleKit"

        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 ABUIKit_ABStyleKit")
    }()
}

But sometimes it won't. Why is that and how can I make it work automatically again (Without the need to manually implement that.)

Both situations happen on Xcode 12 beta.3

Update

Sometimes it shows:

'module' is inaccessible due to 'internal' protection level

And it's not showing the file at all.


回答1:


SPM generates the resource_bundle_accessor only if the corresponding target contains resources as the argument like:

    .target(
        name: "ChenzookKit",
        dependencies: ["Apollo"],
        resources: [.process("Resources")] // <- `copy` or `process` deson't really matter 
    ),

Also, note that it should be a valid resource path.

AND❗️

The project MUST actaully contains Resources inside the target's Directory!




回答2:


Okay I found a solution, so Swift actually generates the Bundle.module file 🎉

The documentation explicitly states that you have to put your Resources under the folder <project_root>/Sources/<MyTarget>/ since SPM scopes resources by target. The target definition looks then like this for my repo SHSearchBar (compare file structure on Github):

// swift-tools-version:5.3
import PackageDescription


    targets: [
        .target(
            name: "SHSearchBar",
            resources: [.copy("Resources")]
        )
    ]

Target Folder: <project_root>/Sources/SHSearchBar
Resource Folder: <project_root>/Sources/SHSearchBar/Resources

By the way, to build an iOS package from command line you can use this command uses the iOS 14 SDK:

swift build -Xswiftc "-sdk" -Xswiftc "\`xcrun --sdk iphonesimulator --show-sdk-path\`" -Xswiftc "-target" -Xswiftc "x86_64-apple-ios14.0-simulator"

To make my little post here complete I also want to mention that a package using the Bundle.module approach can be integrated in apps that run on iOS <14 too since the generated extension does not contain any new API 👍




回答3:


Bundle.module will only be generated by SwiftPM if the resources in the Package.swift file is not empty and the specified resources actually exist.

So there are two possible reasons why it might not work for you:

  1. You don't have any resources specified in Package.swift. Fix like this:
.target(
    name: "MyLibrary",
    dependencies: [
        /* your dependencies */
    ],
    resources: [
        .copy("JsonData"),
        .process("Assets.xcassets"),
    ]
),
  1. The specified paths don't exist or are empty.

    Fix by checking that you've actually placed your resources inside the Sources/MyLibrary directory. A common mistake is to place them directly to Sources or to a different targets subfolder.



来源:https://stackoverflow.com/questions/63237395/generating-resource-bundle-accessor-type-bundle-has-no-member-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!