为什么 NSPredicate 和 DateComponents 不工作

Why are NSPredicate and DateComponents not working

在这里,我使用 NSPredicate 从 Core Data 使用 NSFetchRequest 获取本周星期一的历史记录。我不知道为什么这不起作用你能帮帮我吗?我星期四有相同的代码,只是工作日不同,但出于某种原因它多次打印出日期这是我的代码,我还附上了控制台:

func getDateMonday(){
    
    let currentDate = Date()
    
    let calendar = Calendar.current
    let weekOfMonth = calendar.component(.weekOfMonth, from: Date())
    let month = calendar.component(.month, from: Date())
    
    let mondayComponentStart = DateComponents(month: month, hour: 0, minute: 0, second: 0, weekday: 2, weekOfMonth: weekOfMonth)
    let mondayComponentEnd = DateComponents(month: month, hour: 23, minute: 59, second: 59, weekday: 2, weekOfMonth: weekOfMonth )
    
    let lastMondayStart = Calendar.current.nextDate(after: currentDate,
                                              matching: mondayComponentStart,
                                              matchingPolicy: .nextTime,
                                              repeatedTimePolicy: .first,
                                              direction: .backward) ?? Calendar.current.nextDate(after: currentDate, matching: mondayComponentStart, matchingPolicy: .nextTime, repeatedTimePolicy: .first, direction: .forward)
    let lastMondayEnd = Calendar.current.nextDate(after: currentDate,
                                              matching: mondayComponentEnd,
                                              matchingPolicy: .nextTime,
                                              repeatedTimePolicy: .first,
                                              direction: .forward) ?? Calendar.current.nextDate(after: currentDate, matching: mondayComponentEnd, matchingPolicy: .nextTime, repeatedTimePolicy: .first, direction: .backward)
    
    
    
    let predicateStart = NSPredicate(format: "date >= %@", lastMondayStart! as NSDate)
    let predicateEnd = NSPredicate(format: "date <= %@", lastMondayEnd! as NSDate)
    
    let allPredicate = NSCompoundPredicate(type: .and, subpredicates: [predicateStart, predicateEnd])
    
    let waterFetch = NSFetchRequest<Water>(entityName: "Water")
    waterFetch.predicate = allPredicate

    do {
    print(lastMondayStart!)
    print(Date())
    print(lastMondayEnd!)
    print("------------------")
    let waterF = try viewContext.fetch(waterFetch)
      
            for all in waterF {
                
                let tot = all.amount
                monday += Int(tot)
                Total += Int(tot)
                
    }

    } catch {
        
    }
}

好吧,这个问题很愚蠢,因为我输错了一个函数,我不小心调用了一个函数两次