简体   繁体   English

带有NSDate的NSPredicate按日/月而不是年进行过滤

[英]NSPredicate with NSDate filtering by day/month but not year

I need to create a predicate which needs to filter a list of objects using the property creationDate which is a NSDate object. 我需要创建一个谓词,该谓词需要使用作为NSDate对象的属性creationDate来过滤对象列表。 I want to obtain the list of objects that have the same day and month, but not the same year. 我想获取具有相同日期和月份但不具有相同年份的对象的列表。 Practically, I want the objects that occurred today (as day/month) in the past. 实际上,我想要今天(按天/月)过去发生的对象。 How can I create this predicate? 如何创建该谓词? For example : today is July 27th 2016 and I want all the objects that have July 27th as creationDate. 例如:今天是2016年7月27日,我希望所有具有7月27日作为creationDate的对象。

First, to extract a day and month from a date... 首先,从日期中提取日期和月份...

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
NSInteger day = [components day];
NSInteger month = [components month];

Next, (one way) to build a predicate... 接下来,(一种方式)构建谓词...

[NSPredicate predicateWithBlock:^BOOL(id object, NSDictionary * bindings) {
    // object must be cast to the type of object in the list
    MyObject *myObject = (MyObject *)object;
    NSDate *creationDate = myObject.creationDate;

    // do whatever you want here, but this block must return a BOOL
}];

Putting those ideas together... 将这些想法放在一起...

- (NSPredicate *)predicateMatchingYearAndMonthInDate:(NSDate *)date {
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
    NSInteger day = [components day];
    NSInteger month = [components month];

    return [NSPredicate predicateWithBlock:^BOOL(id object, NSDictionary * bindings) {
        MyObject *myObject = (MyObject *)object;
        NSDate *creationDate = myObject.creationDate;
        NSDateComponents *creationComponents = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:creationDate];
        NSInteger creationDay = [creationComponents day];
        NSInteger creationMonth = [creationComponents month];
        return day == creationDay && month == creationMonth;
    }];
}

That's it. 而已。 Build the predicate with some NSDate who's day and month you want to match in the array, then run that predicate on the array ( filteredArrayUsingPredicate: ). 使用要在数组中匹配的日期和月份的NSDate构建谓词,然后在数组上运行该谓词( filteredArrayUsingPredicate:

NSDate *today = [NSDate date];
NSPredicate *predicate = [self predicateMatchingYearAndMonthInDate:today];
NSArray *objectsCreatedToday = [myArray filteredArrayUsingPredicate:predicate];

Here is the Swift version: 这是Swift版本:

import Photos

func predicateMatchingYearAndMonth(in date: Date?) -> NSPredicate? {
    var components: DateComponents? = nil
    if let date = date {
        components = Calendar.current.dateComponents([.day, .month, .year], from: date)
    }
    let day = components?.day
    let month = components?.month

    return NSPredicate { (date:Any?, [String : Any]?) -> Bool in
        var creationDate:Date?
        var creationComponents: DateComponents? = nil

        if let date = date as? PHAsset {
            creationDate = date.creationDate
        }
        if let creationDate = creationDate {
            creationComponents = Calendar.current.dateComponents([.day, .month, .year], from: creationDate)
        }
        let creationDay: Int? = creationComponents?.day
        let creationMonth: Int? = creationComponents?.month
        return day == creationDay && month == creationMonth
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM