0

I am trying to store only the Twitter id's from the following json data:

( { id = chuckschumer; type = Facebook; }, { id = SenSchumer; type = Twitter; }, { id = SenatorSchumer; type = YouTube; } ),

I am working in objective-C. How would I pull only the id's that have a type = Twitter? Any advice is appreciated.

I then attempted to use NSPredicate but my filtered array continues to return as nil. Here is my code:

NSPredicate *filter = [NSPredicate predicateWithFormat:@"(type = 'Twitter')"];
NSArray *filteredItems = [channels filteredArrayUsingPredicate:filter];
NSLog(@"%@", filteredItems);

filteredItems continues to return nil, not sure why

John
  • 188
  • 1
  • 3
  • 10
  • possible duplicate of [How can I get all values for specific key from each NSDictionary in an NSArray?](http://stackoverflow.com/questions/15018037/how-can-i-get-all-values-for-specific-key-from-each-nsdictionary-in-an-nsarray) – Lord Zsolt Dec 31 '14 at 18:19

1 Answers1

0

To start, get just the items that have type=Twitter:

NSPredicate *filter = [NSPredicate predicateWithFormat:@"(type = 'Twitter')"];
NSArray *filteredItems = [items filteredArrayUsingPredicate:filter];

Then you can iterate through the items to pull the id...

(I just saw this as a related answer, haven't tried it though)

NSArray *idArray = [filteredItems valueForKey: @"id"];
Jeffrey Berthiaume
  • 4,054
  • 3
  • 35
  • 45
  • My filteredItems array returns as nil. Not sure why the filter is not catching anything. The filter is not nil. Any advice? – John Dec 31 '14 at 19:16