2014-03-06 144 views
0

这是我在保存在文档目录中的旧阵列1,阵列2是从服务器获取。在这里,我必须更新阵列1的各个数据与阵列从服务器上获取数据后2如何从另一个阵列更新一个阵列

enter image description here

和更新阵列1之后将是:

enter image description here

+0

好吧,那么你有什么尝试?它做错了什么? – Wain

回答

0

下面是如何比较两个数组并更新本地数组的示例。请记住,此代码可能可以优化,但至少应该给你一个如何去做的想法:-)

- (NSArray *)updateArray:(NSArray *)currentArray withData:(NSArray *)webServerData { 
    NSMutableArray *arr = [[NSMutableArray alloc] init]; 

    //Loop through each NSDictionary in the local array 
    for (NSDictionary *dict in currentArray) { 
     NSString *name = [dict objectForKey:@"NAME"]; 
     BOOL updateDict = NO; 

     //For each NSDictionary, loop through the NSDictionaries in the array from the web server 
     for (NSDictionary *dict2 in webServerData) { 

      //If the name in the local dict is the same as the one in the one from the web server, check if the age is different 
      if ([name isEqualToString:[dict2 objectForKey:@"NAME"]]) { 
       if ([[dict objectForKey:@"AGE"] integerValue] != [[dict2 objectForKey:@"AGE"] integerValue]) { 

        //If the age is different, add the new dictionary 
        [arr addObject:dict2]; 
        updateDict = YES; 
       } 

       break; 
      } 
     } 

     //Add the dict from local array if no new data was found in the web server array 
     if (!updateDict) { 
      [arr addObject:dict]; 
     } 
    } 

    return [arr copy]; 
} 
0

不能写入plist包含在你的包中。如果您需要plist可编辑,您需要在文档目录中创建自己的plist并使用该目录。

流量:

  1. 在首次启动时您的应用程序,您可以通过捆绑的plist,以创建一个保存在文档目录,从原来的plist内容的新的plist。

  2. 每当您需要plist的数据时,请使用您在启动时创建并存储在Documents目录中的数据。切勿再次使用包中的包。

  3. 当您需要更新plist时,请更新您存储在文档中的那个。

+0

我很清楚保存文件并从Doc Dir检索数据。在这里,我只需要排序和智能的方式来映射两个不同的数组,我更新我的问题,可能会帮助你得到我的实际需求。谢谢 – kulss

+0

嗯,所以现在你的问题(如何合并两个数组与自定义对象)是完全不同于你的原始(如何更新本地plist文件)。你需要做的是遍历一个数组,并将每个对象与另一个数组中的对象进行比较。如果你的数据结构像草图,Jorn的答案是一种可能性。我建议你多花一点时间在下一次表达你的问题:-) – Moonwalkr