通过单个属性比较NSSets [英] Comparing NSSets by a single property
问题描述
我试图确定两个NSSets是否相等,但不是在isEqualToSet的意义。两个集合中的项目是相同的类,但不是相同的对象,甚至是对同一对象的引用。他们有有一个属性是相同的 - 我们称之为'name'。
我最好的比较这两套做一个简单的集计数测试,然后一个更复杂的objectsPassingTest:在一个集合中的每个项目,确保一个项目具有相同的名称是在另一个集合?
你可以调用 valueForKey: c> if([[set1 valueForKey:@name)。
//这些集合符合您的条件
}
I'm trying to determine if two NSSets are "equal" but not in the sense of isEqualToSet. Items in the two sets are the same class but are not the same object, or even references to the same object. They will have one property that is the same though - let's call it 'name'.
Is my best bet in comparing these two sets to do a simple set count test, then a more complex objectsPassingTest: on each item in one set, making sure an item with the same name is in the other set? I'm hoping that something simpler exists to handle this case.
You could just call valueForKey:
on both sets and compare the results.
if ([[set1 valueForKey:@"name"] isEqualToSet:[set2 valueForKey:@"name"]]) {
// the sets match your criteria
}
这篇关于通过单个属性比较NSSets的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!