首页 > 解决方案 > 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"]),
    ]
)

标签: iosswiftxcodeswiftuiswift-package-manager

解决方案


为了解决我的问题,我创建了一个 .xcworkspace 来包含我的“测试应用程序”和我的 Swift 包。为此,您可以执行以下步骤:

  1. 创建一个新工作区并在 Xcode 中打开它。
  2. 在工作区中,通过转到 File > New > Project... 创建一个新项目
  3. 创建单个视图应用程序并将其添加到您创建的工作区。
  4. 将测试应用程序添加到您的工作区后,您将需要添加 Swift 包,您可以通过转到文件 > 新建 > Swift 包...
  5. 再次将 Swift 包添加到您创建的工作区,就像您使用测试应用程序一样,如步骤 3 所示。
  6. 您现在将在工作区中看到应用程序和包,但是我们仍然需要修改应用程序的 .xcodeproj 以使用该包。

在此处输入图像描述.

  1. 为此,请选择您的应用程序,然后在“常规”选项卡中的“目标”下,您将看到“框架”、“库”和“嵌入式内容”,单击 + 符号,系统将提示您可以添加到项目中的框架和库,选择您的迅捷包。

在此处输入图像描述

  1. 您现在将看到 Swift 包作为添加的库。

在此处输入图像描述

边注

每当更新 Swift 包时,您都需要在测试应用程序中使用它之前重新构建它。


推荐阅读