Skip to content

Instantly share code, notes, and snippets.

@alexeckermann
Created August 16, 2010 13:40
Show Gist options
  • Save alexeckermann/526955 to your computer and use it in GitHub Desktop.
Save alexeckermann/526955 to your computer and use it in GitHub Desktop.
int (^minusOne)(int);
minusOne = ^(int myNumber) {
return myNumber - 1;
};
NSLog(@"%d", minusOne(3)); // Will print: 2
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/mm/yyyy"];
NSDate *referenceDate = [dateFormatter dateFromString:@"16/08/2010"];
NSArray *otherDates = [NSArray arrayWithObjects:@"12/08/2010", @"25/10/1990", @"30/08/2010", @"25/12/2010", nil];
NSPredicate *findFutureDates = [NSPredicate predicateWithBlock: ^BOOL(id obj, NSDictionary *bind){
return [referenceDate earlierDate: [dateFormatter dateFromString: (NSString *)obj]] == referenceDate;
}];
NSArray *futureDates = [otherDates filteredArrayUsingPredicate: findFutureDates];
[dateFormatter release];
NSLog(@"Block Predicate Arrays ======");
NSLog(@"Dates: %@", otherDates);
NSLog(@"Future Dates: %@", futureDates);
These files are apart of a tutorial on AlexEckermann.com
NSArray *fruits = [NSArray arrayWithObjects:@"Apple", @"Crabapple", @"Watermelon", @"Lemon", @"Raspberry", @"Rockmelon", @"Orange", @"Lime", @"Grape", @"Kiwifruit", @"Bitter Orange", @"Manderin", nil];
NSPredicate *findMelons = [NSPredicate predicateWithFormat:@"SELF contains[cd] 'melon'"];
NSArray *melons = [fruits filteredArrayUsingPredicate:findMelons];
NSPredicate *findApple = [NSPredicate predicateWithFormat:@"SELF beginswith 'Apple'"];
NSArray *apples = [fruits filteredArrayUsingPredicate:findApple];
NSPredicate *findRNotMelons = [NSPredicate predicateWithFormat:@"SELF beginswith 'R' AND NOT SELF contains[cd] 'melon'"];
NSArray *rNotMelons = [fruits filteredArrayUsingPredicate:findRNotMelons];
NSLog(@"Fruits: %@", fruits);
NSLog(@"Melons: %@", melons);
NSLog(@"Apples: %@", apples);
NSLog(@"RNotMelons: %@", rNotMelons);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment