Ios 如何从一个数组更新另一个数组
这是我保存在文档目录中的旧数组1,数组2是从服务器获取的。在这里,在使用数组2从服务器获取数据之后,我必须更新数组1的相应数据 更新后,阵列1将为:Ios 如何从一个数组更新另一个数组,ios,iphone,arrays,Ios,Iphone,Arrays,这是我保存在文档目录中的旧数组1,数组2是从服务器获取的。在这里,在使用数组2从服务器获取数据之后,我必须更新数组1的相应数据 更新后,阵列1将为: 您无法写入捆绑包中包含的plist。如果需要plist可编辑,则需要在documents目录中创建自己的plist并使用该plist 流量: 在第一次启动应用程序时,从捆绑包中获取plist并创建一个新的plist,该plist保存在Documents目录中,内容来自原始plist 每当您需要plist中的数据时,请使用启动时创建并存储在Docu
您无法写入捆绑包中包含的plist。如果需要plist可编辑,则需要在documents目录中创建自己的plist并使用该plist 流量:
下面是一个如何比较两个数组并更新本地数组的示例。请记住,这段代码可能是可以优化的,但它至少应该让您了解如何进行优化:-)
好的,那你试过什么?它做错了什么?我很清楚如何保存文件并从docdir检索数据。在这里,我只需要排序和智能的方式来映射两个不同的数组,我更新我的问题,它可能会帮助你得到我的实际需求。ThanksHmm,所以您现在的问题(如何将两个数组与自定义对象合并)与原来的问题(如何更新本地plist文件)完全不同。您需要做的是迭代一个数组,并将每个对象与另一个数组中的对象进行比较。如果您的数据结构与草图类似,Jorn的答案是一种可能性。我建议你下次多花一点时间来表达你的问题:-)
- (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];
}