如何使用新密钥将数据添加到nsmutablearray?

时间:2012-10-24 04:20:55

标签: json nsmutablearray nsarray nsmutabledictionary

我是iphone开发的新手。最近我开始了一个项目。在JSON解析后我得到了一个

NSmutablearray
(
    {
    "contest_description" = "  ";
    "contest_id" = 245;
    "contest_name" = "teating1 ";
    "contest_status" = Running;
    "creator_id" = "1716091396 ";
    "end_time" = "2012-10-26 04:10:25";
    "full_name" = "Adit Hasan ";
    "no_of_contestants" = 3;
    "no_of_days" = 5;
    "start_time" = "2012-10-21 04:10:24";
    "time_stamp" = "2012-10-24 04:11:09";
    "winner_user_id" = "Not Yet";
    },
    {
    "contest_description" = "  ";
    "contest_id" = 248;
    "contest_name" = "hhhhhhk ";
    "contest_status" = Running;
    "creator_id" = "1716091396 ";
    "end_time" = "2012-10-25 13:33:35";
    "full_name" = "Adit Hasan ";
    "no_of_contestants" = 2;
    "no_of_days" = 4;
    "start_time" = "2012-10-21 13:33:34";
    "time_stamp" = "2012-10-23 13:35:09";
    "winner_user_id" = "Not Yet";
    },
)

如何添加updated_image之类的新密钥并为此密钥分配值?

1 个答案:

答案 0 :(得分:0)

基本上你所拥有的是NSArray NSDictionary,我不知道它是Mutable这就是为什么我将它们称为普通的NS不可变类,如果它们是不可变,您需要将它们转换为Mutable个对象。如下所示

NSMutableArray *mutableArray = [NSMutableArray arrayWithArray:yourPreviousArray];

NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionaryWithDictionary:previousDictionary];

如何,这就是我认为你的问题的解决方案是,我正在考虑已经存在的数组(如果不是,那就没关系,只要你不添加新的词典)和字典是可变的

for (NSMutableDictionary *dictionary in previousArray) {
    [dictionary setValue:updated_image forKey:@"updated_image"];
}

如果您想稍后设置新密钥updated_image的值,请将其初始化为NULL并稍后再设置。

为什么不仔细阅读以下内容

NSArray Class Reference

NSMutableArray Class Reference

NSDictionary Class Reference

NSMutableDictionary Class Reference

还有那里的示例程序。