【问题标题】:how to add data into nsmutable array with a new key?如何使用新键将数据添加到 nsmutablearray 中?
【发布时间】:2012-10-24 04:20:54
【问题描述】:

我是 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 之类的新键并为该键赋值?

【问题讨论】:

    标签: json nsmutablearray nsarray nsmutabledictionary


    【解决方案1】:

    基本上你所拥有的是NSDictionaryNSArray,我不知道是不是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

    还有那里的示例程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多