在一个NSDictionary中合并两个NSDIctionary

时间:2012-10-28 11:38:30

标签: iphone objective-c xcode ios4 nsdictionary

我想在一个NSDictionary中合并两个NSDictionary。 我的第一个DIctionary

BreakFast Dates Dic {
    "2012-07-24" = "27";
    "2012-08-03" = "98";
    "2012-08-06" = "4203";
    "2012-08-23" = "0";
    "2012-08-24" = "0";
    "2012-09-11" = "36";
    "2012-09-19" = "450";
    "2012-09-24" = "36";
    Goals = 3;
}

我的第二次Dictonary

Dinner Dates Dic {
    "2012-08-03" = "100";
    "2012-08-27" = "0";
    "2012-09-19" = "270";
    Goals = 4;
}

我想将两个字典合并为一个。就像在早餐我有“2012-09-19”和我在晚餐时的同一天。我想用逗号(,)分隔这两个值。 我的词典应该是这样的。

combine {
    "2012-07-24" = "27,0";
    "2012-08-03" = "98,100";
    "2012-08-06" = "4203,0";
    "2012-08-23" = "0,0";
    "2012-08-24" = "0,0";
    "2012-09-11" = "36,0";
    "2012-09-19" = "450,270";
    "2012-09-24" = "36,0";
    Goals = 3,4;
}

1 个答案:

答案 0 :(得分:0)

NSMutableDictionary *combined = [objectDelegate.dataBreakFastSummary mutableCopy];
    for(NSString *key in [combined allKeys])
    {
        NSString *breakfastDate = [objectDelegate.dataBreakFastSummary valueForKey:key];
        NSString *dinnerDate = [objectDelegate.dataDinnerSummary objectForKey:key];
        if(dinnerDate){
            [combined setObject:[NSString stringWithFormat:@"%@,%@",breakfastDate, dinnerDate] forKey:key];
            } else {
                [combined setObject:[NSString stringWithFormat:@"%@,0",breakfastDate] forKey:key];
                }
    }
    NSLog(@"Combine %@",combined);