Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 为什么NSPredicate和DateComponents不工作_Swift_Core Data_Nspredicate - Fatal编程技术网

Swift 为什么NSPredicate和DateComponents不工作

Swift 为什么NSPredicate和DateComponents不工作,swift,core-data,nspredicate,Swift,Core Data,Nspredicate,在这里,我使用NSPredicate通过NSFetchRequest从核心数据获取本周的周一历史记录。我不知道为什么这样不行你能帮我吗?我周四有相同的代码,只是工作日不同,但出于某种原因,它多次打印日期。这是我的代码,我还附加了控制台: func getDateMonday(){ 让currentDate=Date() 让calendar=calendar.current 让weekOfMonth=calendar.component(.weekOfMonth,from:Date()) 让mon

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

func getDateMonday(){
让currentDate=Date()
让calendar=calendar.current
让weekOfMonth=calendar.component(.weekOfMonth,from:Date())
让month=calendar.component(.month,from:Date())
让mondayComponentStart=DateComponents(月:月,小时:0,分钟:0,秒:0,工作日:2,周:月:周)
让mondayComponentEnd=DateComponents(月:月,小时:23,分钟:59,秒:59,工作日:2,周:月:周)
让lastMondayStart=Calendar.current.nextDate(在:currentDate之后,
匹配:mondayComponentStart,
匹配策略:。下一次,
RepeatedTime策略:。首先,
方向:。向后)??日历.current.nextDate(之后:currentDate,匹配:mondayComponentStart,匹配策略:.nextTime,RepeatedTime策略:.first,方向:.forward)
让lastMondayEnd=Calendar.current.nextDate(在:currentDate之后,
匹配:mondayComponentEnd,
匹配策略:。下一次,
RepeatedTime策略:。首先,
方向:。前进)??日历.current.nextDate(之后:currentDate,匹配:mondayComponentEnd,匹配策略:.nextTime,repeatedTimePolicy:.first,方向:.backward)
让predicateStart=NSPredicate(格式:“日期>=%@”,lastMondayStart!作为NSDate)

让predicateEnd=NSPredicate(格式:“date好的,所以这个问题非常愚蠢,因为我错误地键入了其中一个函数,并且我意外地调用了一个函数两次。3月4日的下一个星期一(
lastMondayEnd
)是2022年。添加
DateComponents(day:1,second:-1)更容易一些
到开始日期或在开始日期后查找
23:59:59
谢谢您的帮助,但是您知道为什么星期四在控制台中出现两次,因为如果我删除它,它将是另一天出现两次,特别是从后面开始的第三次。2022年有几个日期没有意义。请检查您的设计。
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 {
        
    }
}