Copying a dictionary with multiple sub dictionaries and only returning certain keys from the sub dictionaries

戏子无情 提交于 2019-12-08 05:37:52

问题


In my current iPhone project, I have a created a dictionary that groups the sub dictionaries by the first letter of the "Name" key. NSLog returns the following. I would like to create an identical dictionary that only shows the "Name" key under each initial letter key. What is the best way for making a copy of some of the items in the sub dictionaries? The ObjectForKey methods will select only the items for the initial letter (ex: "B" or "C"). Please let me know if I didn't explain this clearly enough. Thanks!

sectionedDictionaryByFirstLetter:{
B =     (
            {
        Name = "B...A Name Starting with B";
        Image = "ImageName1.png";
        Text = "Some Text";
     }
 );
C =     (
            {
        Name = "C...A Name Starting with C";
        Image = "ImageName2.png";
        Text = "Some Text";
     }
 );
N =     (
            {
        Name = "N...A Name Starting with N";
        Image = "ImageName3.png";
        Text = "Some Text";

     },
            {
        Name = "N...A Name Starting with N";
        Image = "ImageName4.png";
        Text = "Some Text";

     },
            {
        Name = "N...A Name Starting with N";
        Image = "ImageName5.png";
        Text = "Some Text";

     }
   );
}

The final result I'm looking for is:

sectionedDictionaryByFirstLetter:{
B =     (
            {
        Name = "B...A Name Starting with B";
     }
 );
C =     (
            {
        Name = "C...A Name Starting with C";
     }
 );
N =     (
            {
        Name = "N...A Name Starting with N";
     },
            {
        Name = "N...A Name Starting with N";
     },
            {
        Name = "N...A Name Starting with N";
     }
   );
}

回答1:


NSMutableDictionary* newDict = [NSMutableDictionary dictionary];
for (NSString* key in sectionedDictionaryByFirstLetter) {
    NSMutableArray* newList = [NSMutableArray array];
    [newDict setObject:newList forKey:key];
    for (NSDictionary* entry in [sectionedDictionaryByFirstLetter objectForKey:key]) {
        NSString* name = [entry objectForKey:@"Name"];
        [newList addObject:[NSDictionray dictionaryWithObject:name forKey:@"Name"]];
    }
}



回答2:


Core Foundation has a great function, CFDictionaryApplyFunction (I wish they ported its functionality to NSDictionary too). However, NSDictionary and CFDictionary are "toll-free bridges", meaning that you can cast between them at no cost.

So, the solution would be to create an applier function, SaveName, which would be used in the CFDictionaryApplyFunction above. Example:

void SaveName(const void* key, const void* value, void* context) {
  NSMutableDictionary* result = (NSMutableDictionary*) context;
  NSDictionary* dataDict = (NSDictionary*) value;
  NSString* letterKey = (NSString*) key;
  [result setObject:[dataDict valueForKey:@"Name"] forKey:@letterKey];
}

void main() {

   NSDictionary* exampleDict = .. ;
   NSMutableDictionary* resultDict = [NSMutableDictionary dictionary];
   CFDictionaryApplyFunction((CFDictionaryRef)exampleDict, SaveName, (void*)resultDict);
   // now, the resultDict contains key-value pairs of letter-name!
}

My code assumes that there is only one object in the "value" dictionary. You can change SaveName to take into consideration your own data structure, but that's basically the way to do it.



来源:https://stackoverflow.com/questions/1301371/copying-a-dictionary-with-multiple-sub-dictionaries-and-only-returning-certain-k

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!