Package.swift - 复制的资源去哪里?

发布于 2025-01-11 08:11:37 字数 1427 浏览 1 评论 0原文

假设我有一个包含以下内容的 Package.swift

 targets: [
     .target(
         name: "SomeTarget",
         dependencies: [],
         path: "SomePath",
         resources: [.copy("../Assets")]
     )
 ]

并且 Assets 文件夹包含:

  • myImage.png 和
  • myFile.xib

我如何访问这些文件?

这里有一个类似的问题,它引用了Bundle.module,但如果我引用它,我收到 Type 'Bundle' has no member 'module' 错误)。

我尝试枚举所有搜索 xib 的包:

    var targetBundle:Bundle
    let allBundles = Bundle.allBundles

    // Set as the first
    targetBundle = allBundles.first!

    for tempBundle in allBundles {
        if let path = tempBundle.path(forResource: "myFile", ofType: "xib")
        {
            if(path.lengthOfBytes(using: String.Encoding.utf8) > 0)
            {
                targetBundle = tempBundle
                break
            } // End of we found one
        }
        else if let path = tempBundle.path(forResource: "myFile", ofType: "nib")
        {
            if(path.lengthOfBytes(using: String.Encoding.utf8) > 0)
            {
                targetBundle = tempBundle
                break
            } // End of we found one
        }
    }

但没有找到它。如果我在文件系统上打开 .app 包,我也无法在任何地方找到资产 - 我什至在主应用程序包中提取了 Assets.car 文件以查看它们是否存在于其中,但无济于事。

我在哪里可以找到这些资产?

Say I have a Package.swift with the following:

 targets: [
     .target(
         name: "SomeTarget",
         dependencies: [],
         path: "SomePath",
         resources: [.copy("../Assets")]
     )
 ]

And the Assets folder contains:

  • myImage.png and
  • myFile.xib

How can I access these files?

(There is a similar question here which references Bundle.module, but if I reference that, I get a Type 'Bundle' has no member 'module' error).

I've tried enumerating all the bundles searching for the xib:

    var targetBundle:Bundle
    let allBundles = Bundle.allBundles

    // Set as the first
    targetBundle = allBundles.first!

    for tempBundle in allBundles {
        if let path = tempBundle.path(forResource: "myFile", ofType: "xib")
        {
            if(path.lengthOfBytes(using: String.Encoding.utf8) > 0)
            {
                targetBundle = tempBundle
                break
            } // End of we found one
        }
        else if let path = tempBundle.path(forResource: "myFile", ofType: "nib")
        {
            if(path.lengthOfBytes(using: String.Encoding.utf8) > 0)
            {
                targetBundle = tempBundle
                break
            } // End of we found one
        }
    }

But it doesn't get found. If I open the .app package on the file system, I also cannot find the assets anywhere - I even extracted the Assets.car file in the main app bundle to see if they exist within that, but to no avail.

Where can I find these assets?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿童谣 2025-01-18 08:11:37

当 swift 包有 resources 时,并且资源被放置在正确的目录中(位于 Sources 文件夹下的 target 文件夹中),SPM构建后会自动创建一个 resource_bundle_accessor.swift 并将其添加到模块中。该文件为Bundle正确添加了一个静态module,您实际上可以在DerivedSources中找到该文件。
如果出现无模块错误,将资源文件夹移动到正确的位置可能会修复它。在您的情况下,将 Package.swift 中的资源更改为 .copy("Assets"),并将 Assets 放在 Sources/SomeTarget 下

When a swift package has resources, and the resources is put in the right directory(which is in the target folder under Sources folder), SPM will automatically create a resource_bundle_accessor.swift after building and add it to the module. The file adds a static module properly for Bundle, you can actually find the file in DerivedSources.
If you get the no module error, moving your resource folder to the right position will probably fix it. In your case, change resources in Package.swift to .copy("Assets"), and put Assets under Sources/SomeTarget.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文