简体   繁体   中英

NSMutableDictionary and NSMutableArray object in object

I'm smashing my head with a NSDictionary and NSArray add objects for keys stuff.

I got 2 mutable arrays and I want to combine them.

NSMutableArray 1 :

 {
        email = "a@a1.com";
        "password" = "2017-05-23 13:08:03";
    }

NSMutableArray 2

(
        {
        "created_at" = "2017-06-22 00:00:00";
        "id_weather_city" = 1;
        name = Cityname;
        "updated_at" = "2017-06-22 00:00:00";
    },
        {
        "created_at" = "2017-06-22 00:00:00";
        "id_weather_city" = 2;
        name = Cityname2;
        "updated_at" = "2017-06-22 00:00:00";
    }
)

I want to combine them together like this, but I don't know how right array :

{
    email = "a@a1.com";
    "password" = "2017-05-23 13:08:03";
    "weather":(
        {
        "created_at" = "2017-06-22 00:00:00";
        "id_weather_city" = 1;
        name = Cityname;
        "updated_at" = "2017-06-22 00:00:00";
    },
        {
        "created_at" = "2017-06-22 00:00:00";
        "id_weather_city" = 2;
        name = Cityname2;
        "updated_at" = "2017-06-22 00:00:00";
    }
)
}

Try this :

NSMutableDictionary *finalDictionary =  [[array1 objectAtIndex : 0] mutableCopy];
[finalDictionary setValue : array2 forKey:@"weather"];

The first one isn't an array, it's a dictionary. Knowing that you can do

NSMutableDictionary *dict = [item1 mutableCopy];
dict[@"weather"] = [item2 copy];

Simply copy the first dictionary, and insert the second one for weather key

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM