ios - Swift 包中的 SwiftUI 热重载出现“noWorkspaceArena”错误
问题描述
我正在尝试构建一个利用 SwiftUI 创建图表数据可视化的 Swift 包。我的理解是,我可以在开发期间使用 SwiftUI 在 Xcode 中的热重载来预览我的包中的组件,但是当我尝试恢复预览时,我收到错误“无法在此文件中预览 -发生意外错误”诊断程序显示以下错误。
错误:
UVKit.XcodeWorkspaceBuildEnvironment.(未知上下文 $141a48360).(未知上下文 $141a48368).ValidationError.noWorkspaceArena)
GenericHumanReadableError:发生意外错误
noWorkspaceArena
这就是我的看法。
PieChartView.swift:
import SwiftUI
public struct PieChartView : View {
public var data: [(Double, Color)]
public var title: String
public init(data: [(Double, Color)], title: String) {
self.data = data
self.title = title
}
public var body: some View {
Text(title)
}
}
#if DEBUG
struct PieChartView_Previews : PreviewProvider {
static var previews: some View {
PieChartView(data:[(56.0, Color.red),(78, Color.blue),(53, Color.green)], title: "Title")
}
}
#endif
这是我的包裹
包.swift:
import PackageDescription
let package = Package(
name: "MesmerCharts",
platforms: [
.iOS(.v13)
],
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "MesmerCharts",
targets: ["MesmerCharts"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
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 which this package depends on.
.target(
name: "MesmerCharts",
dependencies: []),
.testTarget(
name: "MesmerChartsTests",
dependencies: ["MesmerCharts"]),
]
)
解决方案
为了解决我的问题,我创建了一个 .xcworkspace 来包含我的“测试应用程序”和我的 Swift 包。为此,您可以执行以下步骤:
- 创建一个新工作区并在 Xcode 中打开它。
- 在工作区中,通过转到 File > New > Project... 创建一个新项目
- 创建单个视图应用程序并将其添加到您创建的工作区。
- 将测试应用程序添加到您的工作区后,您将需要添加 Swift 包,您可以通过转到文件 > 新建 > Swift 包...
- 再次将 Swift 包添加到您创建的工作区,就像您使用测试应用程序一样,如步骤 3 所示。
- 您现在将在工作区中看到应用程序和包,但是我们仍然需要修改应用程序的 .xcodeproj 以使用该包。
- 为此,请选择您的应用程序,然后在“常规”选项卡中的“目标”下,您将看到“框架”、“库”和“嵌入式内容”,单击 + 符号,系统将提示您可以添加到项目中的框架和库,选择您的迅捷包。
- 您现在将看到 Swift 包作为添加的库。
边注
每当更新 Swift 包时,您都需要在测试应用程序中使用它之前重新构建它。
推荐阅读
- java - 如何使用appium调用adb命令?
- oracle - Oracle排名查询
- javascript - 如何将JSF中的对象列表传递给javascript函数
- android - HelloCordova 在我的 Android 手机上错误卸载但需要重新安装,在 CLI 中运行的 Cordova 不这样做
- javascript - 检查一个数组是否包含 JavaScript 中另一个数组的任何元素,并返回两个数组中都不存在的值
- python - Numpy 索引顺序
- makefile - 更改openwrt编译包的默认目录src
- python - 如何在 Python 中仅在特定点进行平滑样条插值设置导数?
- java - 如何查看 myBatis 真正使用的 SQL 查询?
- windows - 在 windows 中启动 mongod 服务