当我的代码从其他Swift包导入控件或值时,我在Swift包中的视图中的SwiftUI预览出现问题。
import Foundation
import SwiftUI
import Common
struct AppointmentListItem: View {
var appointment: Appointment
var body: some View {
VStack{
HStack(spacing: 10){
//Client Info
Image(self.appointment.client.profilePicture)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 35, height: 35)
.clipShape(Circle())
.shadow(radius: 10)
.overlay(Circle().stroke(Color.white, lineWidth: 1.5))
Text(self.appointment.client.fullName)
.font(.system(size: 18))
.bold()
.frame(maxWidth: .infinity, alignment: .leading)
Text(self.appointment.getHourAndMinutes()).bold()
//Detail info
Button(action: {
withAnimation{
print("Go to details")
}
}){
Image(systemName: "ellipsis")
.font(.system(size: 18))
.frame(width: 20, height: 20)
.rotationEffect(Angle.init(degrees: 90))
}
}
.padding()
}
.foregroundColor(Color.white)
.background(RoundedRectangle(cornerRadius: 20)
.fill(Color.hippoPrimary)// <- this color is part of Common package
)
}
}
如果我删除或更改.fill(Color.hippoPrimary)
,则预览可用。
Xcode提供的错误如下:
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
这是我的包. swift文件:
// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "TodayAppointments",
platforms: [
.iOS(.v13)
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "TodayAppointments",
targets: ["TodayAppointments"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(path: "../Common")
],
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 this package depends on.
.target(
name: "TodayAppointments",
dependencies: ["Common"]),
.testTarget(
name: "TodayAppointmentsTests",
dependencies: ["TodayAppointments"]),
]
)
在公用程序包中,颜色定义如下:
public extension Color {
static let hippoPrimary = Color("Primary", bundle: .module)
static let progressBarBackground = Color("ProgressBarBackground", bundle: .module)
static let textBackground = Color("TextBackground", bundle: .module)
static let textColor = Color("TextColor", bundle: .module)
static let appleSignInBackground = Color("AppleSignInBackground", bundle: .module)
static let buttonActionText = Color("Text", bundle: .module)
}
构建没有错误,所以我理解依赖关系是好的,听起来像一个IDE。
先谢了。
3条答案
按热度按时间jaxagkaj1#
适用于iOS和macOS的变通方案(未使用Catalyst进行测试):
然后将对
Bundle.module
的调用替换为Bundle.swiftUIPreviewsCompatibleModule
。5ssjco0h2#
抱歉,我上一篇文章不准确。这是Xcode中的一个bug,没有解决办法。刚刚向苹果提交了一个bug报告(FB8880328)。此外,还发布了一个详细的示例代码w/repro步骤here。直接链接到GitHub项目:https://github.com/ryanholden8/SwiftUI-Preview-Failing-Test-Project
做同样的事情,把颜色放在一个单独的包里,也得到了这个错误。这篇文章帮助我找到了问题的根源。我删除了颜色包中生成的默认类。但是,我没有删除基于那个默认类的单元测试。
Common
包中自动生成的单元测试。或者确保所有单元测试都通过。sr4lhrrt3#
这在Xcode 14.2中很管用