NSArray 中字典对象的比较过滤 [英] Filtration by Comparison of Dictionary Objects in NSArray

查看:34
本文介绍了NSArray 中字典对象的比较过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组字典,我需要以非常具体的方式对其进行过滤.用例子来解释最容易.

I have an array of dictionaries that I need to filter in a very specific way. It's easiest to explain by example.

假设我有一个包含以下三个字典的数组,其中@"threadId"、@"subject"、@"messageId" 是键:

Let's say I have an array containing the following three dictionaries where @"threadId", @"subject", @"messageId" are keys:

NSDictionary #1:
@"threadId" : @"1234"
@"subject" :  @"hello"
@"messageId" : @"0001"

NSDictionary #2:
@"threadId" : @"1234"
@"subject" :  @"hello"
@"messageId" : @"0002"

NSDictionary #3:
@"threadId" : @"9101"
@"subject" :  @"goodbye"
@"messageId" : @"0005"

我正在考虑任何对@"threadId" 和@"subject" 具有相同值的字典都是重复的,即使@"messageId" 是不同的.因此,我认为字典 1 和字典 2 是重复的,并且我想从上面的数组中删除字典 1 或字典 2(不是两者).换句话说,我想将所有三个字典的原始数组过滤成一个包含字典 1 和 3 或字典 2 和 3 的新数组.

I'm considering any dictionary that has the same value for @"threadId" and @"subject" to be duplicates, EVEN IF the @"messageId" is different. Thus, I'd consider dictionary 1 and dictionary 2 to be duplicates, and I'd like to remove EITHER dictionary 1 OR dictionary 2 (not both) from the above array. In other words, I'd like to filter the original array of all three dictionaries into a new array containing EITHER dictionaries 1 and 3 OR dictionaries 2 and 3.

到目前为止,我所做的所有尝试都导致了过多的 for 循环,在这种循环中,我尝试按 threadId 对字典进行排序和分离……但随后我陷入了比较部分.我看过按谓词过滤,但看起来它只删除满足特定条件的对象,而与其他对象的比较无关.NSSet 不起作用,因为我认为是重复的对象实际上并不是重复的.

All of my attempts to do this so far have resulted in excessive for loops, in which I attempt to sort and separate dictionaries by threadId... but then I get stuck at the comparison part. I've looked at filtering by predicate but that looks like it only removes objects that meet a particular criteria that has nothing to do with comparison with other objects. NSSet won't work because the objects I'm considering to be duplicates aren't actually duplicates.

我想知道是否有人可以建议执行此过滤的一般策略.

I'm wondering if anyone could suggest a general strategy for performing this filtration.

推荐答案

此代码首先对您的字典进行排序,然后检查其是否重复

This code order your dictionaries firstly and after check if its repeated or no

NSSortDescriptor *sortDescriptor;
//Order by threadId
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"threadId"
                                              ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];

NSArray *arrayOrdered = [yourArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
NSMutableArray *arrayResult = [NSMutableArray array];
NSString* thread = nil;
NSString* subj = nil;
for (NSDictionary*dic in arrayOrdered) {

    if ([thread length] == 0 && [subj length] == 0) {
        thread = [dic objectForKey:@"threadId"];
        subj = [dic objectForKey:@"subject"];
    }else{

        if ([thread isEqualToString:[dic objectForKey:@"threadId"]]) {
            if (![subj isEqualToString:[dic objectForKey:@"subject"]]) {
                //We save it
                [arrayResult addObject:dic];
            }else{
                //It´s already kept
                NSLog(@"repeated dic");
            }
        }else{
            [arrayResult addObject:dic];
        }
    }
}

这篇关于NSArray 中字典对象的比较过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆