IOS:如何将2个词典加入1个词典?

时间:2015-12-10 04:56:55

标签: ios objective-c nsdictionary

在我的项目中,我应用了以下代码

NSDictionary *dict6 = [self cleanJsonToObject:responseData];
    NSLog(@"str : %@",dict6);


    diagnosisdict = [[[dict6 objectForKey:@"diagnoses"] objectAtIndex:0] objectForKey:@"DiagnosesHospitals"];

    diagnosedictforname = [[[dict6 objectForKey:@"diagnoses"]objectAtIndex:0]objectForKey:@"Diagnoses"];
        NSLog(@" for ref id =%@ ,name of diagnose=%@   data is= %@",refidstr,diagnosedictforname ,diagnosisdict);

并且控制台中的输出以

形式出现
str : {
    diagnoses =     (
                {
            Diagnoses =             {
                "diagnosis_name" = "TRANSIENT ISCHEMIA";
            };
            DiagnosesHospitals =             {
                "charge_amt" = "1300.00";
                discharges = "11200.00";
                "hospital_id" = 3341;
                id = 163080;
                "medicare_amt" = "100.00";
                "total_amt" = "1100.00";
            };
        }
    );
    response = 200;
}

ref id =3341 ,name of diagnose={
    "diagnosis_name" = "TRANSIENT ISCHEMIA";
}   data is= {
    "charge_amt" = "1300.00";
    discharges = "11200.00";
    "hospital_id" = 3341;
    id = 163080;
    "medicare_amt" = "100.00";
    "total_amt" = "1100.00";
}

现在我只想将两个字典的值嵌入到一个字典中 有人请帮我解决这个问题。

2 个答案:

答案 0 :(得分:3)

制作第一本字典的可变副本:

NSMutableDictionary * mutDic = [dic1 mutableCopy];

然后:

[mutDic addEntriesFromDictionary:dic2];

答案 1 :(得分:0)

试试这段代码:

NSDictionary *dict6 = [self cleanJsonToObject:responseData];
NSLog(@"str : %@",dict6);


NSMutableDictionary *diagnosisdict = [[[dict6 objectForKey:@"diagnoses"] objectAtIndex:0] objectForKey:@"DiagnosesHospitals"];

NSDictionary *diagnosedictforname = [[[dict6 objectForKey:@"diagnoses"]objectAtIndex:0]objectForKey:@"Diagnoses"];

NSArray *keys = [diagnosedictforname allKeys];
for (int i =0; i < keys.count; i++) {
    NSString *key = [keys objectAtIndex:i];
    [diagnosisdict setValue:[diagnosedictforname valueForKey:key] forKey:key];
}

NSLog(@"your dic -> %@", diagnosisdict);