Created
February 4, 2012 09:42
-
-
Save tilomitra/1736780 to your computer and use it in GitHub Desktop.
Categorizing Facebook Likes into an NSMutableDictionary
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
/* | |
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