Getting Error: Type '()' cannot conform to 'View'

can't see what the problem is .. Im getting the error: Type '()' cannot conform to 'View'

struct CalendarView: View {
    @StateObject private var viewModel = CalendarViewModel()
    @State private var selectedDate: CalendarDate?
    @State private var showModal = false
    
    var body: some View {
        VStack {
            Text("Calendar App")
                .font(.largeTitle)
                .padding()
            
            GridStack(rows: 5, columns: 7) { row, col in
                let index = row * 7 + col
                if index < viewModel.calendarDates.count {
                    let calendarDate = viewModel.calendarDates[index]
                    Text("\(Calendar.current.component(.day, from: calendarDate.date))")
                        .frame(width: 40, height: 40)
                        .background(calendarDate.isSelected ? Color.blue : Color.clear)
                        .cornerRadius(10)
                        .foregroundColor(calendarDate.isSelected ? Color.white : Color.black)
                        .onLongPressGesture {
                            selectedDate = calendarDate
                            showModal = true
                        }
                }
            }
        }
        .background(Color.gray)
        .sheet(isPresented: $showModal) {
            if let date = selectedDate {
                DateSelectionModal(selectedDate: date)
            }
        }
    }
}

struct GridStack<Content: View>: View {
    let rows: Int
    let columns: Int
    let content: (Int, Int) -> Content
    
    var body: some View {
        VStack {
            ForEach(0..<rows) { row in
                HStack {
                    ForEach(0..<columns) { column in
                        content(row, column)
                    }
                }
            }
        }
    }
}

#Preview {
    CalendarView()
}

@donnist78 use @ViewBuilder let content: (Int, Int) -> Content instead.

use ViewBuilder to construct views from closures.

Guessing the issue is here:

.sheet(isPresented: $showModal) {
  if let date = selectedDate {
    DateSelectionModal(selectedDate: date)
  }
}

because you're not returning a view - you're doing an if statement.

You should really determine whether the sheet should be displayed and then set the showModal value. So, in your code, change this:

.onLongPressGesture {
  selectedDate = calendarDate
  showModal = true
}

to this:

.onLongPressGesture {
  selectedDate = calendarDate
  if let date = selectedDate {
    showModal = true
  }
}
Getting Error: Type '()' cannot conform to 'View'
 
 
Q