먼저,
[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
}];
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
NSInteger day = [components day];
NSInteger month = [components month];
다음, 술어를 구축하기 (편도)은 ... 함께 그 아이디어를 퍼팅 ... 날짜에서 하루 달 추출. ..
- (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];
predicateWithBlock을 사용하여 NSPredicate를 만들었지 만, 예를 들어 Photos 프레임 워크에서 지원되지 않는 경우가 있습니다. 블록 사용을 피하면서 동일한 결과를 얻으려면 어떻게해야합니까? –