大佬教程收集整理的这篇文章主要介绍了从嵌套的 Swift 包访问资产时,SwiftUI 预览会崩溃 一个模块B 模块? 错误:那是为什么?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个包含两个结构体的模块 (A)。
public struct Codecolor {
public init() { }
public let value = SwiftUI.color(#colorliteral(red: 0.8549019694,green: 0.250980407,blue: 0.4784313738,Alpha: 1))
}
public struct Assetcolor {
public init() { }
public let value = SwiftUI.color("Legacy/Material/Gold",bundle: .modulE)
}
预览效果很好:
@H_616_21@
第二个模块 (B) 应使用前一个 (A) 作为依赖项从以下位置加载颜色:
import A
public struct Codecolor {
public init() { }
public var value: SwiftUI.color { A.Codecolor().value }
}
public struct Assetcolor {
public init() { }
public var value: SwiftUI.color { A.Assetcolor().value }
}
但是一旦它触及模块 (A) 中的资产,预览就会崩溃:
无法在此文件中预览。未能更新预览。
RemoteHumanReadableError: Failed to update prevIEw.
The prevIEw process appears to have crashed.
Error encountered when sending 'prevIEwInstances' message to agent.
==================================
| RemoteHumanReadableError: The operation Couldn’t be completed. (BSserviceConnectionErrorDomain error 3.)
|
| BSserviceConnectionErrorDomain (3):
| ==BSErrorCodeDescription: OperationFailed
注意:奇怪的是,如果 B 预览代码在实际应用中(而不是另一个包)中,它可以正常工作
Here is the full code on github
我在开发者论坛中找到了解决方案:
链接
https://developer.apple.com/forums/thread/664295?login=true#reply-to-this-question
注意 - 本地
解决方案是本地包。你定义
let bundlenameIOS = "LocalPackages_TargetName"
let bundlenameR_661_11845@acOs = "Packagename_TargetName"
远程(例如来自 GitHub 的包)
如果你在GitHub上有你的包并远程获取它,你不能定义本地包,你必须稍微改变它:
let bundlenameIOS = "TargetName_TargetName"
let bundlenameR_661_11845@acOs = "TargetName_TargetName"
示例
这是一个完整的实现示例,您必须将其与资源一起放入包中:
// Inside your package with the resources:
// Extend Bundle to access it in other packages
extension Bundle {
// public static let assets = Bundle.module
// updated with workaround
public static let assets = Bundle.mymodule
}
定义您的捆绑包
private class CurrentBundleFinder {}
extension Foundation.bundle {
static var mymodule: Bundle = {
/* The name of your package. You may have same Packagename and TargetName*/
let bundlenameIOS = "TargetName_TargetName"
let bundlenameR_661_11845@acOs = "TargetName_TargetName"
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: CurrentBundleFinder.self).resourceURL,// -> Optional UI Tests
/* Bundle should be present here when the package is used in UI Tests. */
Bundle(for: CurrentBundleFinder.self).resourceURL?.deletingLastPathComponent(),/* For command-line tools. */
Bundle.main.bundleURL,/* Bundle should be present here when running previews from a different package (this is the path to "…/Debug-iphonesimulator/"). */
Bundle(for: CurrentBundleFinder.self).resourceURL?.deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent(),Bundle(for: CurrentBundleFinder.self).resourceURL?.deletingLastPathComponent().deletingLastPathComponent(),]
for candidate in candidates {
let bundlePathiOS = candidate?.appendingPathComponent(bundlenameIOS + ".bundle")
let bundlePathMacOS = candidate?.appendingPathComponent(bundlenameR_661_11845@acOs + ".bundle")
if let bundle = bundlePathiOs.flatMap(Bundle.init(url:)) {
return bundle
} else if let bundle = bundlePathMacOs.flatMap(Bundle.init(url:)) {
return bundle
}
}
fatalError("unable to find bundle")
}()
}
访问包 B 中的包 A 资源
您现在可以从包 B 中的包 A 访问资源,例如:
let example = UIImage(named: "Exampe",in: Bundle.assets,with: nil)!
以上是大佬教程为你收集整理的从嵌套的 Swift 包访问资产时,SwiftUI 预览会崩溃 一个模块B 模块? 错误:那是为什么?全部内容,希望文章能够帮你解决从嵌套的 Swift 包访问资产时,SwiftUI 预览会崩溃 一个模块B 模块? 错误:那是为什么?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。