调用没有参数的表达式中缺少参数参数 #1

Missing Argument Parameter #1 in Call for an expression with no parameters

提问人:Complete Athletic 提问时间:12/13/2022 更新时间:12/13/2022 访问量:79

问:

当我尝试在锻炼列表中调用 ForEach 时,我不断收到错误“调用中缺少参数 #1 的参数”,我不知道 Swift 对什么感到困惑。我相信它与它引用的 ViewModel 有关,但我不确定如何明确地告诉它。

这是我的视图代码:


struct WorkoutProgramView: View {
    @ObservedObject var workoutViewModel: WorkoutViewModel
    @StateObject var vm: WorkoutListViewModel = .init()
    @State var currentWeek: [Date] = []
    @State var currentDay: Date = Date()
    @State var selection: String? = nil
    let screenSize: CGRect = UIScreen.main.bounds
    let columns = [GridItem(.adaptive(minimum: 150, maximum: 400))]

var body: some View {

ScrollView{
                LazyVGrid(columns: columns){
                    ForEach(vm.workoutDays, id:\.id) { day in
                        if isSameDay(date1: currentDay, date2: day.date!){
                            ZStack{
                                RoundedRectangle(cornerRadius: 5).frame(width: .infinity).padding().foregroundColor(Color.black).opacity(0.1)
}

我在 ForEach 行上收到错误。

这是我的 workoutViewModel 和 vm:

import SwiftUI
@MainActor
class WorkoutListViewModel: ObservableObject{
    @ObservedObject var workoutViewModel = WorkoutViewModel()
    @Published var workoutDays: [WorkoutDays] = []
    
    func loadWorkoutData() async throws{
        let svc = NestedFirestoreService(workoutViewModel: workoutViewModel)

        self.workoutDays = try await svc.retrieveWorkoutDays()
    }
}


class WorkoutViewModel: ObservableObject {
    let auth = Auth.auth()
    let user = Auth.auth().currentUser
    @Published var name: String = ""
    @Published var date: Date = Date()
    @Published var exercise: Double = 0.0
    @Published var workoutProgramList: [WorkoutProgramModel] = []
    @Published var todaysFoodList: [FoodItem] = []
    @Published var workoutProgramSelected: String = ""
    @Published var workoutDayData: [WorkoutDays] = []
    @Published var workoutDaySelected: String = ""
    @Published var exerciseData: [ExerciseModel] = []
    @Published var ExerciseSelected: String = ""
    @Published var loggedSet: Int = 0
    @Published var loggedReps: Int = 0
    @Published var loggedWeight: Int = 0
    @Published var notes: String = ""
    @Published var dayAndExerciseList: [newStrengthModel] = []
}

这是我的 WorkoutDays 模型“:

struct WorkoutDays: Encodable, Decodable, Identifiable {
    @DocumentID var id: String?
    var name: String?
    var date: Date?
    var description: String?
    var timeToCompleteWarmup: Double?
    var timeToCompleteStrength: Double?
    var timeToCompleteMetcon: Double?
    var timeToCompleteAccessories: Double?
    var exercises: [ExerciseModel]?
}
struct ExerciseModel: Encodable, Decodable, Hashable, Identifiable {
    @DocumentID var id: String?
    var workoutDayID: String?
    var name: String?
    var reps: [Int]?
    var percent: [Double]?
    var linkToVideo: String?
    var isCompleted: Bool?
    var description: String?
    var timeToComplete: Double?
    var type: String? 
}

谁能帮忙?已经坚持了好几天了,它肯定会让我烦恼,哈哈。

iOS SwiftUI 参数传递 实例

评论

1赞 jnpdx 12/13/2022
您在哪一行收到错误?
0赞 jnpdx 12/13/2022
顺便说一句,与您的问题无关,但 SwiftUI 并不真正支持嵌套 s。此外,您不应该在 -- 该属性包装器用于ObservableObject@ObservedObjectclassView
1赞 vadian 12/13/2022
我怀疑这个问题是由可选的属性包装器引起的。@DocumentID
0赞 Complete Athletic 12/13/2022
@jnpdx在“ForEach”行上
0赞 Complete Athletic 12/13/2022
@vadian,你为什么认为这是问题所在,你有什么想法如何解决吗?

答: 暂无答案