2009-10-22 9 views
4

NSMutableArray의 코어 데이터에서 항목을로드했습니다. 각 항목은 생성 될 때 만기 날짜가 주어지며, 사용자가 선택할 수 있습니다.NSArray를 NSDate, 오늘으로 정렬합니다.

어떻게 분류하면 오늘 만기일 인 항목 만 표시됩니까?

NSPredicate *predicate = [NSPredicate predicateWithFormat: @"dueDate == %@", [NSDate date]]; 

[allObjectsArray filterUsingPredicate: predicate]; 

이 코드는하지만 작동하지 않습니다 여기에

는 내가 지금까지 무엇을 가지고 있습니다. 어떤 제안

답변

0

방법 -filterUsingPredicate:에 대한

덕분에 전용 (유형 NSMutableArray의) 변경 가능한 배열에서 작동합니다.

대신 -filteredArrayUsingPredicate: 방법을 사용해보십시오 :

NSString *formattedPredicateString = [NSString stringWithFormat:@"dueDate == '%@'", [NSDate date]]; 
NSPredicate *predicate = [NSPredicate predicateWithFormat:formattedPredicateString]; 
NSArray *filteredArray = [allObjectsArray filteredArrayUsingPredicate:predicate]; 
1

술어를 사용하여 문제는 그들이 표준 날짜 비교를 사용하는 경우에만 정확하게 날짜 과의 시간이다 날짜를 반환 할 것이다 주어진 날짜. 당신이 "오늘"날짜를 원하는 경우에, 당신은 다음과 같이 (있는 NSDate 확장으로 가능) 어딘가에 -isToday 방법을 추가해야합니다 :

-(BOOL)dateIsToday:(NSDate *)aDate { 

    NSDate *now = [NSDate date]; 

    NSCalendar *cal = [NSCalendar currentCalendar]; 
    NSDateComponents *nowComponents = [cal components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit 
             fromDate:now]; 

    NSDateComponents *dateComponents = [cal components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit 
              fromDate:aDate]; 

    return (([nowComponents day] == [dateComponents day]) && 
     ([nowComponents month] == [dateComponents month]) && 
     ([nowComponents year] == [dateComponents year])); 

} 

당신이이 사람을 찾을 수있을만큼 간단한 것이있어 일단 오늘 그 :

NSMutableArray *itemsDueToday = [NSMutableArray array]; 

for (MyItem *item in items) { 
    if ([self dateIsToday:[item date]) { 
     [itemsDueToday addObject:item]; 
    } 
} 

// Done! 
+0

이 접근 방식은 효과가 있지만, 항목 당 많은 날짜 처리 및 계산이 필요합니다. 그래서 몇 백 개 이상의 항목이 있다면 처리 시간이 합산되기 시작할 것입니다. –

+0

그 외에도 추가 비용에 대해 알고 있어야합니다. http://www.mikeabdullah.net/NSCalendar_currentCalendar.html –

12

어떻게 그냥 00:00 오늘을 계산하고 내일 00:00 다음 (> = 및 <)에게 술어에 날짜를 비교에 대해. 따라서 모든 날짜 객체는 '오늘'로 분류되는 두 날짜 내에 있어야합니다. 이를 위해서는 배열에 몇 개의 날짜 객체가 있더라도 처음에는 2 개의 날짜 만 계산하면됩니다.

// Setup 
NSCalendar *cal = [NSCalendar currentCalendar]; 
NSDate *now = [NSDate date]; 

// Get todays year month and day, ignoring the time 
NSDateComponents *comp = [cal components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:now]; 

// Components to add 1 day 
NSDateComponents *oneDay = [[NSDateComponents alloc] init]; 
oneDay.day = 1; 

// From date & To date 
NSDate *fromDate = [cal dateFromComponents:comp]; // Today at midnight 
NSDate *toDate = [cal dateByAddingComponents:oneDay toDate:fromDate options:0]; // Tomorrow at midnight 

// Cleanup 
[oneDay release] 

// Filter Mutable Array to Today 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"dueDate >= %@ && dueDate < %@", fromDate, toDate]; 
NSArray *filteredArray = [allObjectsArray filteredArrayUsingPredicate:predicate]; 

// Job Done! 
+5

+1 아마도 가장 좋은 옵션 일 것입니다. NSPredicate * predicate = [NSPredicate predicateWithFormat : @ "dueDate BETWEEN % @", [NSArray arrayWithObjects : fromDate, toDate, nil]];'(Notice 'predicateWithFormat :'을 사용할 때'predicateString' 객체를 만들지 않아도 됨) –

+0

+1에 감사드립니다! 네, 'BETWEEN' 키워드 사용에 대해 생각했지만 toDate가 자정의 다음 날이기 때문에 술어가 그 날짜보다 작지 만 같지 않기를 바랬습니다. 내가 올바르게 기억하면 'BETWEEN'은 상한과 하한을 포함합니다. 또한 당신은'predicateWithFormat :'에 대해 꽤 옳습니다! –