You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI日期选择视图报错:No exact matches in call to initializer

解决SwiftUI ForEach初始化报错及日历展示逻辑问题

报错原因

  1. 语法转义错误:代码中使用了HTML转义字符&lt;(对应<)和&gt;(对应>),导致Swift编译器无法正确解析ForEach(0..&lt;daysInMonth)和函数返回类型-&gt; some View,触发No exact matches in call to initializer错误。
  2. 日期逻辑错误:当前代码从currentDate(今天)开始累加天数,会展示从今天到未来的日期,而非当月的所有日期。
  3. 性能问题:在循环内重复创建DateFormatter,会造成不必要的性能开销。

修复步骤

  • 替换所有HTML转义字符:将&lt;改为<,&gt;改为>。
  • 修正日期计算逻辑:先获取当月第一天,再基于第一天累加天数生成当月所有日期。
  • 优化DateFormatter:将其定义为静态属性,避免重复初始化。
  • 调整@ViewBuilder代码位置:将日期文本构建逻辑移出循环闭包,提升代码可读性。

修复后的完整代码

import SwiftUI
import Foundation

struct DateSelectionView: View {
    let calendar = Calendar.current
    let currentDate = Date()
    static let dayFormatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "dd"
        return formatter
    }()
    
    // 获取当月第一天
    var firstDayOfMonth: Date {
        calendar.date(from: calendar.dateComponents([.year, .month], from: currentDate))!
    }
    
    // 获取当月总天数
    var daysInMonth: Int {
        calendar.range(of: .day, in: .month, for: firstDayOfMonth)!.count
    }
    
    @ViewBuilder
    private func dayText(for 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(Self.dayFormatter.string(from: date))
        }
    }
    
    var body: some View {
        ScrollView(.horizontal, showsIndicators: false) {
            HStack(spacing: 20) {
                ForEach(0..<daysInMonth, id: \.self) { dayOffset in
                    let date = calendar.date(byAdding: .day, value: dayOffset, to: firstDayOfMonth)!
                    dayText(for: date)
                        .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()
    }
}

额外说明

  • 静态dayFormatter只初始化一次,提升循环渲染性能。
  • firstDayOfMonth确保从当月第一天开始生成日期,正确展示整月的所有天数。
  • 将dayText函数提取为私有方法,让代码结构更清晰,符合SwiftUI编码规范。

内容的提问来源于stack exchange,提问作者EMPIRE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 23:26:07