Example of filtering an array using predicateWithBlock


/ Published in: Objective C
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.