Created
August 17, 2013 12:03
-
-
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...)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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