【问题标题】:Remove duplicate from nsmutablearray containing nsdictionary从包含 nsdictionary 的 nsmutablearray 中删除重复项
【发布时间】:2014-11-13 08:25:22
【问题描述】:

我想从 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;
    }
)

【问题讨论】:

  • 这就是创建自定义模型对象如此有价值的原因。在这种情况下,您可以覆盖 isEqual 并将对象放入一个集合中以删除欺骗。过度使用字典意味着代码比必要的复杂得多。
  • 你想要 row_id = 1 表示重复中的最新 id

标签: ios nsmutablearray nsmutabledictionary


【解决方案1】:

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

解决方案 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;
}

【讨论】:

  • 没有for循环也可以吗?
  • 我不认为。如果即使有任何内置方法,该方法中也必须有一个内部循环。顺便说一句,为什么不循环?
  • @Shoaib 嗨,我尝试了你的第一个解决方案,但它出现了这样的错误Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<_SwiftValue 0x618000441680> valueForUndefinedKey:]: this class is not key value coding-compliant for the key _id.'
  • @DavidJonker 请确保您使用的密钥名称有效。
【解决方案2】:
    NSMutableSet *keysSet = [[NSMutableSet alloc] init];
    NSMutableArray *filteredArray = [[NSMutableArray alloc]init];
    for (NSDictionary *msg in germanMakes) {
    NSString *key = [NSString stringWithFormat:@"%@", msg[@"act_id"]];
    if (![keysSet containsObject:key]) {
        [filteredArray addObject:msg];
        [keysSet addObject:key];
    }
    }
    NSLog(@"filteredResults %@ keyset%@",filteredArray , keysSet);

germanMakes 是您的初始数组,keysSet 包含唯一的 act_id(例如 1,2,3,4..),filteredArray 是您按 act_id 过滤的数组。请记住,这不会按升序排序,但那部分会很容易。

【讨论】:

    猜你喜欢
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 2013-04-20
    • 2015-12-20
    • 1970-01-01
    相关资源
    最近更新 更多