对象中的NSMutableDictionary和NSMutableArray对象
问题描述:
我用NSDictionary
和NSArray
为我的头粉碎了我的脑袋。对象中的NSMutableDictionary和NSMutableArray对象
我得到2个可变数组,我想合并它们。
的NSMutableArray 1:
{
email = "[email protected]";
"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";
}
)
我想他们这样的结合在一起,但我不知道如何正确 数组:
{
email = "[email protected]";
"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";
}
)
}
答
第一个不是数组,它是一个字典。知道,你可以做
NSMutableDictionary *dict = [item1 mutableCopy];
dict[@"weather"] = [item2 copy];
只需复制的第一部字典,然后将第二个为weather
关键
+0
谢谢,我忘了我可以使用字典添加一个对象[@“name”] –
答
试试这个:
NSMutableDictionary *finalDictionary = [[array1 objectAtIndex : 0] mutableCopy];
[finalDictionary setValue : array2 forKey:@"weather"];
能分享你做了什么? –