gpt4 book ai didi

ios - 从包含 nsdictionary 的 nsmutablearray 中删除重复项

转载 作者:行者123 更新时间:2023-12-01 15:53:15 25 4
gpt4 key购买 nike

我想从 nsmutablearray 中删除重复项。

数组结构:-

(
{
"act_end_date" = "";
"act_entry_date" = "13/11/2014";
"act_recurrrance_type" = Daily;
"act_start_date" = "2014-11-13";
"row_id" = 0;
"act_id" = 2;
}
{
"act_end_date" = "";
"act_entry_date" = "13/11/2014";
"act_recurrrance_type" = Daily;
"act_start_date" = "2014-11-13";
"row_id" = 1;
"act_id" = 2;
}
{
"act_end_date" = "";
"act_entry_date" = "16/11/2014";
"act_recurrrance_type" = Daily1;
"act_start_date" = "2014-11-15";
"row_id" = 2;
"act_id" = 3;
}
)

我想要的是检查数组在哪里包含任何具有相同 act_id 的字典。如果是,则删除第一本词典。即上述数组的预期结果是:-

(
{
"act_end_date" = "";
"act_entry_date" = "13/11/2014";
"act_recurrrance_type" = Daily;
"act_start_date" = "2014-11-13";
"row_id" = 1;
"act_id" = 2;
}
{
"act_end_date" = "";
"act_entry_date" = "16/11/2014";
"act_recurrrance_type" = Daily1;
"act_start_date" = "2014-11-15";
"row_id" = 2;
"act_id" = 3;
}
)

他们有什么办法吗?是否可以使用 NSPREDICATE?

我尝试通过以下方式进行:-

  NSMutableArray *filteredArray = [NSMutableArray array];

for (NSMutableDictionary* E1 in Event_Array)
{

BOOL hasDuplicate = [[filteredArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"act_id == %@", [E1 objectForKey:@"act_id"]]] count] > 0;

if (!hasDuplicate)
{
[filteredArray addObject:E1];
}
}

因为它返回数组:---

(
{
"act_end_date" = "";
"act_entry_date" = "13/11/2014";
"act_recurrrance_type" = Daily;
"act_start_date" = "2014-11-13";
"row_id" = 0;
"act_id" = 2;
}
)

我想要的是

(
{
"act_end_date" = "";
"act_entry_date" = "13/11/2014";
"act_recurrrance_type" = Daily;
"act_start_date" = "2014-11-13";
"row_id" = 1;
"act_id" = 2;
}
)

最佳答案

假设您希望最新的对象出现在列表中。

解决方案 1:(简单)

反转数组

NSMutableArray *filteredArray = [NSMutableArray array];
for (int i = Event_Array.count-1; i>= 0; i--)
{
NSMutableDictionary* E1 = [Event_Array objectAtIndex:i];
BOOL hasDuplicate = [[filteredArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"act_id == %@", [E1 objectForKey:@"act_id"]]] count] > 0;

if (!hasDuplicate)
{
[filteredArray addObject:E1];
}
}

解决方案 2:

使用 NSMutableDictionary 而不是数组。

//Keys are act_id(s)
"2" = {
"act_end_date" = "";
"act_entry_date" = "13/11/2014";
"act_recurrrance_type" = Daily;
"act_start_date" = "2014-11-13";
"row_id" = 1;
"act_id" = 2;
};
"3" = {
"act_end_date" = "";
"act_entry_date" = "16/11/2014";
"act_recurrrance_type" = Daily1;
"act_start_date" = "2014-11-15";
"row_id" = 2;
"act_id" = 3;
}

关于ios - 从包含 nsdictionary 的 nsmutablearray 中删除重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26904295/

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