gpt4 book ai didi

ios - 比较两个 NSArray 并检测变化的最佳方法是什么

转载 作者:行者123 更新时间:2023-11-28 21:39:44 25 4
gpt4 key购买 nike

我的要求是确定地址簿中的更改(哪些联系人和哪些字段自上次打开应用程序以来发生了更改。

现在我有两个 NSArrays 作为 arrSavedContacts(包含以前的地址簿联系人)和 arrContacts(当前地址簿联系人)。这就是两个数组的样子

arrSavedContacts

{
firstName = Gayan;
id = 224;
lastName = Udaha;
phones = (
{
label = Mobile;
value = "123456789";
}
);
},
{
firstName = Chandrananda;
id = 225;
lastName = "";
phones = (
{
label = Mobile;
value = "234567891";
}
);
},
{
firstName = Joe;
id = 228;
lastName = B8;
phones = (
{
label = Mobile;
value = "345678912";
}
);
},

arr联系人

{
firstName = FirstName Changed;
id = 224;
lastName = Udaha;
phones = (
{
label = Mobile;
value = "123456789";
}
);
},
{
firstName = Chandrananda;
id = 225;
lastName = "";
phones = (
{
label = Mobile;
value = "345678912";
}
);
},

对于上面的两个数组,我们可以注意到两个变化,第 3 项被删除,第 1 项中的 firstName 被更改。

由于地址簿中可以有数千个项目,我的问题是,比较这两个数组并检测更改的项目(删除、添加、内部字段的更改)的最佳和最佳方法是什么?

非常感谢代码示例

最佳答案

这是检查更改、添加和删除的代码。请检查,如果您不明白,请评论...

NSMutableArray *oldArr =[NSMutableArray arrayWithArray:@[@{
@"firstName" : @"Gayan",
@"id" : @224,
@"lastName" : @"Udaha",
@"phones": @[
@{
@"label" : @"Mobile",
@"value" : @"123456789"
}
]
},
@{
@"firstName" : @"Chandrananda",
@"id" : @225,
@"lastName" : @"",
@"phones": @[
@{
@"label" : @"Mobile",
@"value" : @"234567891"
}
]
},
@{
@"firstName" : @"Joe",
@"id" : @226,
@"lastName" : @"",
@"phones": @[
@{
@"label" : @"Mobile",
@"value" : @"345678912"
}
]
},
]];

NSMutableArray *newArr =[NSMutableArray arrayWithArray:@[@{
@"firstName" : @"Gayan",
@"id" : @224,
@"lastName" : @"Udaha",
@"phones": @[
@{
@"label" : @"Mobile",
@"value" : @"123456789"
}
]
},
@{
@"firstName" : @"Chandrananda",
@"id" : @225,
@"lastName" : @"",
@"phones": @[
@{
@"label" : @"Mobile",
@"value" : @"11111111"
}
]
},
@{
@"firstName" : @"Luan",
@"id" : @229,
@"lastName" : @"",
@"phones": @[
@{
@"label" : @"Mobile",
@"value" : @"55555555"
}
]
},
]];


NSMutableSet *oldSet = [NSMutableSet setWithArray:oldArr];
NSMutableSet *newSet = [NSMutableSet setWithArray:newArr];

//1: Get filter all changed, add, delete items

[oldSet minusSet:newSet];
[newSet minusSet:oldSet];

NSArray *remainOldArr = [NSArray arrayWithArray:[oldSet allObjects]];
NSArray *remainNewArr = [NSArray arrayWithArray:[newSet allObjects]];

if (remainNewArr.count == 0 && remainOldArr.count == 0) {
// there is no changes.
return ;
}

//2: detect which items changed,add,delete

NSMutableArray *arrayChangedItems = nil;
NSMutableArray *arrayDeleteItems = nil;
NSMutableArray *arrayAddItems =nil;

NSPredicate *pre1 = [NSPredicate predicateWithFormat:@"self.id IN %@",[remainOldArr valueForKeyPath:@"id"]];
NSPredicate *pre2 = [NSPredicate predicateWithFormat:@"NOT self.id IN %@",[remainOldArr valueForKeyPath:@"id"]];
NSPredicate *pre3 = [NSPredicate predicateWithFormat:@"NOT self.id IN %@",[remainNewArr valueForKeyPath:@"id"]];

arrayChangedItems = [NSMutableArray arrayWithArray:[remainNewArr filteredArrayUsingPredicate:pre1]];
arrayAddItems = [NSMutableArray arrayWithArray:[remainNewArr filteredArrayUsingPredicate:pre2]];
arrayDeleteItems = [NSMutableArray arrayWithArray:[remainOldArr filteredArrayUsingPredicate:pre3]];

关于ios - 比较两个 NSArray 并检测变化的最佳方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32663971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com