I'm trying to build a Swift Package that utilizes SwiftUI to create chart data visualizations. My understanding was that I would be able to use SwiftUI's hot-reloading in Xcode to preview the components within my package during development, however when I'm trying to resume the preview, I'm getting the error "Cannot preview in this file - unexpected error occurred" the diagnostics show the following error.
UVKit.XcodeWorkspaceBuildEnvironment.(unknown context at $141a48360).(unknown context at $141a48368).ValidationError.noWorkspaceArena)
GenericHumanReadableError: unexpected error occurred
noWorkspaceArena
This is what my view looks like.
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
Here is my package
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"]),
]
)
To resolve my issue, I created an .xcworkspace to contain my "test application" and my Swift Package. To do this, you can perform the following steps:
Side Note
Whenever the Swift Package is updated, you will need to rebuild it before using it in the test application.