0

我想将两个 NSDictionary 合并到一个 NSDictionary 中。我的第一本词典

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

我的第二本词典

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;
}
4

1 回答 1

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);
于 2012-11-01T13:21:36.013 回答