Skip to content

Instantly share code, notes, and snippets.

@tilomitra
Created February 4, 2012 09:42
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 tilomitra/1736780 to your computer and use it in GitHub Desktop.
Save tilomitra/1736780 to your computer and use it in GitHub Desktop.
Categorizing Facebook Likes into an NSMutableDictionary
/*
This method takes a NSDictionary of likes returned by facebook and returns an NSMutableDictionary of NSArrays, where
each key in the dictionary corresponds with a "category"
For example:
{
"company": [{} , {} , {} ],
"song": [ {} , {} ]
}
*/
- (void) categorizeLikes:(NSDictionary *)likesData {
NSArray *allLikes = (NSArray *)[likesData objectForKey:@"data"];
NSMutableDictionary *sortedLikes = [NSMutableDictionary dictionary];
NSMutableArray *tempArray = [NSMutableArray arrayWithArray:allLikes];
for (likes in tempArray) {
NSString *catKey = [likes objectForKey:@"category"];
//If the dictionary already has this category defined, then there is a NSMutableArray inside it
//and we can just push this object into that array
if ([sortedLikes objectForKey:catKey] != nil) {
[[sortedLikes objectForKey:catKey] addObject:likes];
}
//Otherwise, if the dictionary does not have this category defined,
//we must create a NSMutableArray and add it to this category with that object inside it.
else {
NSMutableArray *catArray = [NSMutableArray arrayWithObject:likes];
[sortedLikes setObject:catArray forKey:catKey];
}
}
self.likes = sortedLikes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment