开发者

How to filter a dictionary of arrays

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] isEqu开发者_Go百科al: [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


    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.


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;
             }
         }];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜