“CalendarViewModel”类型的值没有成员“weeks”

问题描述 投票:0回答:1

我在尝试访问特定类型中不存在的属性或方法时遇到错误。

import SwiftUI

struct CalendarView: View {
    let viewModel = CalendarViewModel()

    var body: some View {
        NavigationView {
            VStack {
                Text("Habits Calendar")
                    .font(.largeTitle)
                    .padding()

                Text(viewModel.startDate.formattedDate())
                    .font(.headline)
                    .padding()

                HStack {
                    Button(action: {
                        viewModel.previousMonth()
                    }) {
                        Image(systemName: "arrowshape.turn.up.left").font(.title)
                    }
                    .foregroundColor(.green)

                    Spacer()

                    Button(action: {
                        viewModel.nextMonth()
                    }) {
                        Image(systemName: "arrowshape.turn.up.right").font(.title)
                    }
                    .foregroundColor(.green)
                }
                .padding(.horizontal)
                .padding()

                HStack(spacing: 10) {
                    ForEach(["Dom", "Seg", "Ter", "Qua", "Qui", "Sex", "Sáb"], id: \.self) { day in
                        Text(day)
                            .font(.headline)
                            .frame(maxWidth: .infinity)
                            .foregroundColor(day == "Dom" || day == "Sáb" ? .red : .primary)
                    }
                }
                .padding(.horizontal)

                ***let weeks = viewModel.weeks***

                LazyVGrid(columns: Array(repeating: GridItem(), count: 7), spacing: 10) {
                    ForEach(weeks) { week in
                        ForEach(week) { date in
                            CalendarCellView(viewModel: viewModel, date: date)
                                .environmentObject(viewModel)
                        }
                    }
                }

                .alert(isPresented: .constant(!viewModel.alertMessage.isEmpty)) {
                    Alert(
                        title: Text("Dia selecionado."),
                        message: Text(viewModel.alertMessage),
                        dismissButton: .default(Text("OK")) {
                            viewModel.alertMessage = ""
                        }
                    )
                }
            }
        }
    }
}

由于我刚刚开始使用代码,我需要一些帮助来解决这个我无法解决的错误,如果有人可以帮助我,我将非常感激。

swift types compiler-errors properties mismatch
1个回答
0
投票
import SwiftUI

class CalendarViewModel: ObservableObject {
    @Published var startDate = Date()
    @Published var selectedDate: Date?
    @Published var specialDates: [Date] = []
    @Published var selectedDates: [Date] = []
    @Published var isEditablePopupShowing = false
    @Published var selectedDay: Date?
    @Published var popupText = ""
    @Published var savedTexts: [String] = []
    @Published var alertMessage = ""
    @Published var popupText1: String = ""
    @Published var popupText2: String = ""
    @Published var isSelected: Bool = false

    func previousMonth() {
        if let previousMonth = startDate.previousMonth() {
            startDate = previousMonth
        }
    }
    func nextMonth() {
        if let nextMonth = startDate.nextMonth() {
            startDate = nextMonth
        }
    }
    func showAlert(message: String) {
        alertMessage = message
    }
}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.