我在代码的View_Preview部分收到以下编译器错误:“调用中缺少参数'userInfo'的参数”。
Xcode建议如下修复:“(userInfo:[Employee])",但此修复程序会出现以下编译器错误:“无法转换类型'[Employee]的值。类型'转换为预期的参数类型'[Employee]'“
下面是我的视图代码:
struct EmployeeView: View {
@EnvironmentObject var pilot: Pilot
let userInfo: [Employee]
var body: some View {
ZStack {
List {
ForEach(userInfo, id: \.age) {line in
EmployeeCardView(employee: line)
}
}
}
}
}
struct EmployeeView_Previews: PreviewProvider {
static var previews: some View {
EmployeeView() //ERROR here
.environmentObject(Pilot)
}
}
下面是我的数据模型代码:
struct Employee: Identifiable, Codable {
var id = UUID()
var age: Int
var yearGroup: Int
var category: String
init(age: Int, yearGroup: Int, category: String) {
self.age = age
self.yearGroup = yearGroup
self.category = category
}
}
@MainActor class Pilot: ObservableObject {
@Published var pilot: Employee
init(pilot: Employee) {
self.pilot = pilot
}
var pilotInfo: [Employee] = [
Employee(age: 35, yearGroup: 1, category: "B717")]
}
我也试过这个:
有没有苹果教程解释View_Previews这方面的逻辑?
1条答案
按热度按时间epfja78i1#
这些错误不是很有用,我相信这是初始化@State和其他参数的问题。没有初始化/默认值-构造函数失败