0

I have a dictionary of arrays which I want to filter based on an element in the arrays. My dictionary looks like this...

"Abu Dhabi, U.A.E." =     (
    "24.466665",
    "54.416668",
    "Asia/Dubai"
);
"Accra, Ghana" =     (
    "5.583333",
    "-0.100000",
    "Africa/Accra"
);
"Adak, America" =     (
    "",
    "",
    "America/Adak"
);
"Addis Ababa, Ethiopia" =     (
    "9.050000",
    "38.700001",
    "Africa/Addis_Ababa"
);

I want to filter the dictionary based on the 3rd item, for example all keys that the third item (timezone ID) is "America/Adak".

I think I can do this with keysOfEntriesPassingTest, but I am at a loss as to how to do this. I have found some sample code...

mySet = [myDict keysOfEntriesPassingTest:^(id key, id obj, BOOL *stop) {
             if( [[obj port] isEqual: [NSNumber numberWithInt: 8080]]) {
                 return YES;
             else
                 return NO;
             }]

But I cannot figure out how to rewrite this to work in my case. I do not understand the syntax required.

Could someone please help me figure out how to implement this filter?

Thanks, John

user278859
  • 10,379
  • 12
  • 51
  • 74

2 Answers2

6
    mySet = [myDict keysOfEntriesPassingTest:^(id key, id obj, BOOL *stop) {
                 if ([[obj objectAtIndex:2] isEqualToString:@"America/Adak"]) {
                     return YES;
                 } else {
                     return NO;
                 } 
                }];

Note that, as the method name implies, the resulting set will only contain the keys, not the arrays themselves.

new2ios
  • 1,350
  • 2
  • 25
  • 56
Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
  • Thanks again. Any chance you could help me understand the syntax of the method. What is keysOfEntriesPassingTest:^(id key, id obj, BOOL *stop) trying to tell me. Obj must be the stored object, but how would you use key and what is BOOL *stop? ok I think I am getting it... I can use key to test the key for a match of some kind like contains or starts with. I think stop can be used to stop the filtering when the first match is found??? Not really sure how to use stop. – user278859 Feb 10 '11 at 01:39
  • 1
    The method `keysOfEntriesPassingTest:` will iterate over your dictionary and call that block of code in curly braces once for every object stored in the dictionary. In the block, you can use `key` to get the `key` of the current value (e.g. `@"Adak, America"` in your case), `obj` is the value itself, and `stop` is a way to tell the method to stop the iteration at a certain point. So yes, it works as you assume. For every object that the block returns `YES`, the method stores the object's key in the set it finally returns to you. – Ole Begemann Feb 10 '11 at 12:13
0

Ole, Thank YOU!!

The if statement in the sample code I found was not formatted correctly. I think you revised only the pertinent portion for me. For the benefit of others looking at this question, here is the final working code...

mySet = [citiesDictionary keysOfEntriesPassingTest:^(id key, id obj, BOOL *stop) 
         {
             if([[obj objectAtIndex:2] isEqualToString:@"America/Adak"]){
                 return YES;

             }else {
                 return NO;
             }
         }];
user278859
  • 10,379
  • 12
  • 51
  • 74