Skip to content

Instantly share code, notes, and snippets.

@indyfromoz
Created August 17, 2013 12:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indyfromoz/6256593 to your computer and use it in GitHub Desktop.
Save indyfromoz/6256593 to your computer and use it in GitHub Desktop.
Snippets of Objective-C code handy for working with collections (Should go in a category, one day...)
// Check if an item is in a NSMutableArray
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"pubDate = %@", tumblrPost.pubDate];
NSArray *filteredArray = [favoriteItems filteredArrayUsingPredicate:predicate];
if (filteredArray.count > 0) {
// item found!
}
// Remove an item from a NSMutableArray
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"createdAt = %@", facebookPost.createdAt];
[favoriteItems removeObjectsInArray:[favoriteItems filteredArrayUsingPredicate:predicate]];
// Sort a NSMutableArray with a NSComparisonResult
NSArray *sortedArray = [favoriteItems sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
NSDate *firstCachedDate = [(RHCacheableBase *)a cachedDate];
NSDate *secondCachedDate = [(RHCacheableBase *)b cachedDate];
return [firstCachedDate compare:secondCachedDate];
}];
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment