I used a ForEach to present the calender. I am missing something in my code and can't place my finger on it. Any help is needed. This is the error: No exact matches in call to initializer
import SwiftUI import Foundation
struct DateSelectionView: View {
let calendar = Calendar.current
let currentDate = Date()
var daysInMonth: Int {
calendar.range(of: .day, in: .month, for: Date())!.count
}
var body: some View {
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 20) {
ForEach(0..<daysInMonth, id: \.self) { day in
let date = calendar.date(byAdding: .day, value: day, to: currentDate)!
let dayText: String
@ViewBuilder
func buildDayText() -> some View {
if calendar.isDateInToday(date) {
Text("Today")
} else if calendar.isDateInYesterday(date) {
Text("Yesterday")
} else if calendar.isDateInTomorrow(date) {
Text("Tomorrow")
} else {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd"
Text(dateFormatter.string(from: date))
}
}
buildDayText()
.font(.title)
.foregroundColor(.blue)
.padding(10)
.background(Color.gray.opacity(0.2))
.cornerRadius(10)
}
}
.padding()
}
}
}
struct DateSelectionView_Previews: PreviewProvider {
static var previews: some View {
DateSelectionView()
}
}
Inside your
ForEachyou have a function declaration, this is why the compiler is getting confused.You just need to re-organize your code and make the function standalone. Since the function is a
ViewBuilderyou probably need to move thedateFormatteroutside of the function too: