复制了多个子字典词典,只从子字典返回某些键 [英] Copying a dictionary with multiple sub dictionaries and only returning certain keys from the sub dictionaries

查看:120
本文介绍了复制了多个子字典词典,只从子字典返回某些键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我当前的iPhone项目,我有一个创建了一个字典,由姓名键的第一个字母组子词典。的NSLog返回以下。我想创建一个相同的字典,只能说明每个初始字母键下的名称键。什么是在子词典使得一些项目的副本的最佳方式?该ObjectForKey方法将选择只针对首字母的项目(例如:B或C)。请让我知道,如果我没有解释不够清楚。
谢谢!

  sectionedDictionaryByFirstLetter:{
B =(
            {
        NAME =B ...一个名称为B开始;
        图片=ImageName1.png;
        文本=一些文本;
     }
 );
C =(
            {
        名称=C ...一个名称用C开始;
        图片=ImageName2.png;
        文本=一些文本;
     }
 );
N =(
            {
        NAME =N ...一个名称以N开始;
        图片=ImageName3.png;
        文本=一些文本;     },
            {
        NAME =N ...一个名称以N开始;
        图片=ImageName4.png;
        文本=一些文本;     },
            {
        NAME =N ...一个名称以N开始;
        图片=ImageName5.png;
        文本=一些文本;     }
   );
}

我在寻找最终的结果是:

  sectionedDictionaryByFirstLetter:{
B =(
            {
        NAME =B ...一个名称为B开始;
     }
 );
C =(
            {
        名称=C ...一个名称用C开始;
     }
 );
N =(
            {
        NAME =N ...一个名称以N开始;
     },
            {
        NAME =N ...一个名称以N开始;
     },
            {
        NAME =N ...一个名称以N开始;
     }
   );
}


解决方案

 的NSMutableDictionary * newDict =的NSMutableDictionary字典]
对于(的NSString *在sectionedDictionaryByFirstLetter键){
    NSMutableArray里* newList = [NSMutableArray的阵列]
    [newDict的setObject:newList forKey:键];
    对(在NSDictionary中*进入[sectionedDictionaryByFirstLetter objectForKey:键]){
        * NSString的名字= [进入objectForKey:@名称];
        [newList ADDOBJECT:[NSDictionray dictionaryWithObject:名称forKey:@名]];
    }
}

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";
     }
   );
}

解决方案

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

这篇关于复制了多个子字典词典,只从子字典返回某些键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆