0

2つのNSDictionaryを1つの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;
}

私の2番目のディクトナリー

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

両方の辞書を1つにマージしたい。朝食に「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 に答える