Posted By

magickaito on 04/11/11


Tagged

objectivec


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

mirage3d
Tintenklecks
cnathan


Example of filtering an array using predicateWithBlock


 / Published in: Objective C
 

  1. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  2. [dateFormatter setDateFormat:@"dd/mm/yyyy"];
  3.  
  4. NSDate *referenceDate = [dateFormatter dateFromString:@"16/08/2010"];
  5.  
  6. NSArray *otherDates = [NSArray arrayWithObjects:@"12/08/2010", @"25/10/1990", @"30/08/2010", @"25/12/2010", nil];
  7.  
  8. NSPredicate *findFutureDates = [NSPredicate predicateWithBlock: ^BOOL(id obj, NSDictionary *bind){
  9. return [referenceDate earlierDate: [dateFormatter dateFromString: (NSString *)obj]] == referenceDate;
  10. }];
  11.  
  12. NSArray *futureDates = [otherDates filteredArrayUsingPredicate: findFutureDates];
  13.  
  14. [dateFormatter release];

Report this snippet  

You need to login to post a comment.