"Cannot convert value of type 'Binding<Date>?' to expected argument type 'Date?'"

71 views Asked by At

It's related to a type mismatch in Swift.

Essentially, the compiler is telling you that there is a discrepancy between the expected types and the provided types in a given context.

import SwiftUI

struct CalendarCellView: View { @ObservedObject var viewModel: CalendarViewModel let date: Date

@State private var isSelected: Bool

// Inicializador
init(viewModel: CalendarViewModel, date: Date, isSelected: Bool = false) {
    self.viewModel = viewModel
    self.date = date
    self._isSelected = State(initialValue: isSelected)
}

var body: some View {
    return CalendarDay(
        date: date,
        isSelected: isSelected,
        isToday: date.isToday(),
        isWeekend: date.isWeekend(),
        isSpecial: date.isSpecial(specialDates: viewModel.specialDates),
        onTap: {
            // Toggle the selection state
            isSelected.toggle()
            
            if isSelected {
                self.viewModel.selectedDates.append(self.date)
                self.viewModel.selectedDate = self.date
                self.viewModel.isEditablePopupShowing = true
            } else {
                self.viewModel.selectedDates.removeAll { $0 == self.date }
                self.viewModel.selectedDate = nil
                
                let message = "Desmarcou o dia \(self.date.formattedDay()) de \(self.date.formattedDate())"
                if !self.viewModel.popupText1.isEmpty {
                    self.viewModel.showAlert(message: message)
                } else {
                    self.viewModel.isEditablePopupShowing = false
                    self.viewModel.showAlert(message: message)
                }
            }
        }
    )
    .overlay(
        Group {
            if isSelected {
                EditablePopupView(
                    viewModel: EditablePopupViewModel(
                        selectedDay: $viewModel.selectedDate.wrappedValue,
                        isEditablePopupShowing: $viewModel.isEditablePopupShowing
                    ),
                    selectedDay: $viewModel.selectedDate,
                    isEditablePopupShowing: $viewModel.isEditablePopupShowing,
                    popupText1: viewModel.popupText1,
                    popupText2: viewModel.popupText2,
                    savedTexts: viewModel.savedTexts
                )
            }
        }
    )



                        .padding()
                        .background(Color.white)
                        .clipShape(RoundedRectangle(cornerRadius: 10))
                        .offset(x: 0, y: -50)
                   }
               }
0

There are 0 answers