Package.swift - 复制的资源去哪里?
假设我有一个包含以下内容的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当 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 aresource_bundle_accessor.swift
after building and add it to the module. The file adds a staticmodule
properly forBundle
, you can actually find the file inDerivedSources
.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 putAssets
underSources/SomeTarget
.