NSDray Sort by NSDate Today

I loaded an element from the master data in NSMutableArray. Each item, when created, is given an assigned date that the user selects.

How to sort, so only the element that should appear today is displayed?

Here is what I got so far:

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

This code does not work.

Thanks for any suggestions.

+4
source share
3 answers

How about what you just calculate today at 00:00, and then tomorrow at 00:00, and then compare the dates in the predicate with those (> = and <). Therefore, all date objects must fall within these two dates, which will be classified as Today. This requires that you first calculate only 2 dates, regardless of how many date objects are in your array.

 // 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! 
+12
source

The problem with using predicates is that if they use standard date comparisons, they will return dates that exactly match the date and time of that date. If you need today's dates, you will need to add the -isToday method somewhere (possibly as an extension for NSDate), for example:

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

Once you have it, just find the ones you have today:

 NSMutableArray *itemsDueToday = [NSMutableArray array]; for (MyItem *item in items) { if ([self dateIsToday:[item date]) { [itemsDueToday addObject:item]; } } // Done! 
+1
source

The -filterUsingPredicate: method works only with mutable arrays (of type NSMutableArray ).

Try using the -filteredArrayUsingPredicate: method, instead:

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

All Articles