NSPredicate与NSDate过滤日/月但不是年

时间:2016-07-27 21:35:55

标签: ios objective-c swift nsdate nspredicate

我需要创建一个谓词,该谓词需要使用属性n = 3 a = [1, 2, 5, 3, -2, -1, 4, 5, 2, 4, 8] b = [abs(i - j) > n for i, j in zip(a[:-1], a[1:])] m = [i + 1 for i, j in enumerate(b) if j is True] m = [0] + m + [len(a)] result = [a[i: j] for i, j in zip(m[:-1], m[1:])] print(result) 来过滤对象列表,该属性是NSDate对象。我想获取具有相同日期和月份但不是同一年的对象列表。实际上,我想要过去发生的今天(日/月)对象。我该如何创建这个谓词?例如:今天是2016年7月27日,我希望所有7月27日的对象都是creationDate。

2 个答案:

答案 0 :(得分:3)

首先,从日期中提取日期和月份......

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

接下来,(单向)构建谓词......

[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
}];

将这些想法放在一起......

- (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;
    }];
}

就是这样。使用在数组中要匹配的日期和月份的NSDate构建谓词,然后在数组上运行该谓词(filteredArrayUsingPredicate:)。

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

答案 1 :(得分:0)

这是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
    }
}