Pregunta

I am trying to combine two NSMutableDictionarys, but the issue I am running into is when I add them it just overwrites the other. For example Dictionary A has the following Data:

{
    StationLogs = (
                {
            log = "This is the Log Text";
            timeStamp = "2014-01-04 04:31:04 +0000";
            title = Title;
        }
    );
}

And Dictionary 2 would have this data:

{
        StationLogs = (
        {
            log = "logText";
            timeStamp = "2014-01-04 04:35:04 +0000";
            title = Title2;
        }
    );
}

How would I get them to merge to look something like this:

    {
        StationLogs = (
        {
            log = "This is the Log Text";
            timeStamp = "2014-01-04 04:31:04 +0000";
            title = Title;
        },
        {
            log = "logText";
            timeStamp = "2014-01-04 04:35:04 +0000";
            title = Title2;
        }
    );
}

I have searched on stack overflow and on the rest of the web, but could not find anything. I am sorry if this was already answered and I could not find it because I was not searching for the correct phrase.

Thanks, Skylar

¿Fue útil?

Solución

You can iterate through each data set and create your merged data set

    NSMutableDictionary *dict1; //Your first set of data
    NSMutableDictionary *dict2; //Your second set of data

    NSArray *keys1 = [dict1 allKeys];
    NSArray *keys2 = [dict2 allKeys];

    NSMutableDictionary *combinedDictionary = [NSMutableDictionary dictionary];

    //Iterating through first data set
    for (id key in keys1) {
        NSArray *array = [dict1 objectForKey:key];

        NSMutableArray *subArray = [combinedDictionary objectForKey:key];
        if (!subArray) {
            subArray = [NSMutableArray array];
            [combinedDictionary setObject:subArray forKey:key];
        }

        [subArray addObjectsFromArray:array];
    }

    //Iterating through second data set
    for (id key in keys2) {
        NSArray *array = [dict2 objectForKey:key];

        NSMutableArray *subArray = [combinedDictionary objectForKey:key];
        if (!subArray) {
            subArray = [NSMutableArray array];
            [combinedDictionary setObject:subArray forKey:key];
        }

        [subArray addObjectsFromArray:array];
    }

Instead of creating a new NSMutableDictionary as combinedDictionary, you can replace merged data in one of the NSMutableDictionary you already have.

Otros consejos

A dictionary is basically a set of key-value pairs. Your Dictionary A and Dictionary 2 illustrate this. But in your combined "dictionary" what you have is a set of two dictionaries, not a set of key-value pairs. In other words it is just not possible to combine dictionaries like that because the result that you are expecting is not a dictionary. If you want a set of dictionaries, use NSSet or NSArray(recommended)

may this help you ...

NSMutableDictionary *dict=[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"This is the Log Text",@"log",@"2014-01-04 04:31:04 +0000",@"timeStamp",@"Title",@"title", nil];

NSMutableDictionary *dict1=[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Log Text",@"log",@"2014-01-04 04:35:04 +0000",@"timeStamp",@"Title2",@"title", nil];

NSArray *arr=[NSArray arrayWithObject:dict];
NSArray *arr1=[NSArray arrayWithObject:dict1];

//here is your first dictnary
NSMutableDictionary *dictm1=[[NSMutableDictionary alloc]initWithObjectsAndKeys:arr,@"StationLogs", nil];


NSLog(@"%@",dictm1);

 //here is your second dictionary
NSMutableDictionary *dictm2=[[NSMutableDictionary alloc]initWithObjectsAndKeys:arr1,@"StationLogs", nil];
NSLog(@"%@",dictm2);

//now here you get array of dictnary which is storted with key value "StationLogs" for first didtnary
NSArray *tempArray=[[NSArray alloc]initWithArray:[dictm1 objectForKey:@"StationLogs"]];
NSLog(@"%@",tempArray);

  //now here you get array of dictnary whaich is storted with key value "StationLogs" for second didtnary
NSMutableArray *tempArray1=[[NSMutableArray alloc]initWithArray:[dictm2 objectForKey:@"StationLogs"]] ;

[tempArray1 addObjectsFromArray:tempArray];

//now adding value of second dictionary to first dict
[dictm1 setValue:tempArray1 forKey:@"StationLogs"];

NSLog(@"%@",dictm1);

and i had made simple if you can make it comact Like coding

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top