如何根据对象的属性比较两个NSSets? [英] How to compare two NSSets based on attributes of objects?

查看:140
本文介绍了如何根据对象的属性比较两个NSSets?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个nssets。

I have two nssets.

nsset1: person.id = 1, person.id = 2, person.id = 3
nsset2: person.id = 1, person.id = 2

结果应该是:

nsset1 - nsset2: person (with id 3)
nsset2 - nsset1: null

这两个集中具有相同id的对象是不同的对象,所以我不能简单地做minusSet。

These objects with the same id in those two sets are different objects, so I couldn't simply do minusSet.

我想做类似的事情:

nsset1: person.id = 1, person.id = 2, person.id = 3
nsset2: person.id = 4, person.id = 5

结果应如下所示:

nsset1 - nsset2: person (id 1), person (id 2), person (id 3)
nsset2 - nsset1: person (id 4), person (id 5)

最好的方法是什么?

推荐答案

你应该尝试这样的事情

NSSet* nsset1 = [NSSet setWithObjects:person_with_id_1, person_with_id_2, person_with_id_3, nil];
NSSet* nsset2 = [NSSet setWithObjects:person_with_id_2, person_with_id_4, nil];

// retrieve the IDs of the objects in nsset2
NSSet* nsset2_ids = [nsset2 valueForKey:@"objectID"]; 
// only keep the objects of nsset1 whose 'id' are not in nsset2_ids
NSSet* nsset1_minus_nsset2 = [nsset1 filteredSetUsingPredicate:
    [NSPredicate predicateWithFormat:@"NOT objectID IN %@",nsset2_ids]];

这篇关于如何根据对象的属性比较两个NSSets?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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