在下面的代码中,我有一个具有不同值的结构。
struct Workout: Codable {
let exercise: [Exercise]
enum CodingKeys: String, CodingKey {
case exercise = "exercise"
}
}
struct Exercise: Codable {
let exercise, set: Int
}
class WorkoutViewModel: ObservableObject {
@Published var workout: Workout
init(_ workout: Workout) {
self.workout = workout
}
}
在主视图中,我将值初始化为:
struct CreateWorkoutView: View {
@ObservedObject var myExercises = WorkoutViewModel(Workout(exercise: [Exercise(exercise: 1, set: 1)]))
var body: some View {
NavigationView {
NavigationLink(
destination: View2(exerciseList: self.$myExercises.workout.exercise)
){
Text("Add Exercises")
}
}
struct View2: View {
@ObservedObject var excersices = getExercisesData()
@Binding var exerciseList: [Exercise]
var body: some View {
VStack {
List(excersices.datas){i in
HStack{
VStack(alignment: .leading){
Text(i.name)
}
Spacer()
VStack(alignment: .leading){
Image(systemName: "info.circle")
}
}.onTapGesture {
self.exerciseList.append(Exercise(exercise: i.exerciseId, set: 1))
}
}
}
}
}
我设置self。$ myExercises.workout.exercise的那一行有问题。 XCode错误显示了一个不同的位置(HStack
),并给出了以下错误,因为它似乎并不相关:
Generic parameter 'C0' could not be inferred
信息:1. In call to function 'buildBlock' (SwiftUI.ViewBuilder)
我想将此练习数组移至下一个视图以向其中添加项目。
知道为什么不允许圆锥的以上部分吗?
谢谢
答案 0 :(得分:0)
错误消息不相关。问题在于let exercise, set: Int
是一个常量而不是一个var。改变解决了问题。
评论出现的错误有时会有助于理解编译器真正抱怨的部分。