starwar2012 2013-01-15 02:52 采纳率: 0%
浏览 2418
已采纳

objectiveC中创建NSMutableArray

有一个NSArray给出下面的数据:

01/14/2013 13:28:06.559 IUser Reader [71164: c07] (
         {
         "id_acompanhante" = "";
         "id_evento" = 34;
         "user_id" = 1;
         "inserido_por" = "Himself";
         name = iUser;
         status = done;
         type = User;
     }
         {
         "id_acompanhante" = 1;
         "id_evento" = 34;
         "user_id" = 1;
         "inserido_por" = iUser;
         name = "Mayara Roca";
         status = naofeito;
         type = companion;
     }
)

想将这些数据在NSMutableArray显示,并且给每个item.example添加另一个field。

  {
     "id_acompanhante" = "";
     "id_evento" = 34;
     "user_id" = 1;
     "inserido_por" = "Himself";
     name = IUser;
     status = done;
     type = User;
     tag = 2;
 }

如何实现?

  • 写回答

1条回答 默认 最新

  • Matt 2013-01-16 05:48
    关注

    两个办法,简单直观的就是利用已有的不可变的数组或者字典创建可变的(mutable)数组字典,再添加新的tag标签.

    NSArray *theOldArray; // your old array
    NSMutableArray *theMutableAry = [NSMutableArray array];
    for (NSDictionary *dicItem in theOldArray)
    {
        NSMutableDictionary *dicWithOneMoreField = [NSMutableDictionary dictionaryWithDictionary:dicItem];
        [dicWithOneMoreField setValue:@"your value for tag" forKey:@"tag"];
        [theMutableAry addObject:dicWithOneMoreField];
    }
    

    如果数据结构很复杂又可能变化,比如数组里有字典,字典里又有数组等等,那就将数据归档再反归档,这样所有的数据就都变成mutable的了,之后随便你怎么操作.

    NSData* archivedData = [NSKeyedArchiver archivedDataWithRootObject:theOldArray];
    NSMutableArray *mutableArray = [NSKeyedUnarchiver unarchiveObjectWithData:archivedData];
    for (NSDictionary *dicItem in mutableArray)
    {
        [dicItem setValue:@"your value for tag" forKey:@"tag"];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果