在 SwiftUI 中创建了一个日期选择视图,但在调用初始化程序时出现了“没有精确匹配”的错误

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

我使用 ForEach 来呈现日历。我的代码中遗漏了一些东西,无法将手指放在上面。需要任何帮助。这是错误:调用初始化程序时没有完全匹配

导入 SwiftUI 进口粉底

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()
    }
}
date swiftui foreach calendar initializer
1个回答
0
投票

在你的

ForEach
里面你有一个函数声明,这就是编译器感到困惑的原因。

您只需要重新组织代码并使该功能独立即可。由于该函数是一个

ViewBuilder
,您可能也需要将
dateFormatter
移到函数之外:

private var dateFormatter: DateFormatter {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "dd"
    return dateFormatter
}

@ViewBuilder
private func buildDayText(date: Date) -> some View {
    if calendar.isDateInToday(date) {
        Text("Today")
    } else if calendar.isDateInYesterday(date) {
        Text("Yesterday")
    } else if calendar.isDateInTomorrow(date) {
        Text("Tomorrow")
    } else {
        Text(dateFormatter.string(from: date))
    }
}

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
                buildDayText(date: date)
                    .font(.title)
                    .foregroundColor(.blue)
                    .padding(10)
                    .background(Color.gray.opacity(0.2))
                    .cornerRadius(10)
            }
        }
        .padding()
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.