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'.
我试图确定两个NSSets是否“相等”但不是isEqualToSet意义上的。两个集合中的项目是相同的类,但不是同一个对象,甚至是对同一个对象的引用。他们将拥有一个相同的属性 - 让我们称之为'名称'。
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.
比较这两组进行简单的设置计数测试,然后是更复杂的objectsPassingTest:在一组中的每个项目上,确保一个具有相同名称的项目在另一组中是我最好的选择吗?我希望能够处理这种情况更简单。
3 个解决方案
#1
2
You could just call valueForKey:
on both sets and compare the results.
您可以在两个集合上调用valueForKey:并比较结果。
if ([[set1 valueForKey:@"name"] isEqualToSet:[set2 valueForKey:@"name"]]) {
// the sets match your criteria
}
#2
3
I had the same problem, but I needed to compare multiple properties at the same time (class User with properties Name and Id).
我有同样的问题,但我需要同时比较多个属性(类User与属性Name和Id)。
I resolved this by adding a method returning an NSDictionary with the properties needed to the class:
我通过添加一个返回NSDictionary的方法来解决这个问题,该方法具有该类所需的属性:
- (NSDictionary *)itemProperties
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:self.name forKey:@"name"];
[dict setObject:self.id forKey:@"id"];
return dict;
}
and then using valueForKey:
as Kevin Ballard mentioned:
然后使用valueForKey:正如Kevin Ballard所说:
BOOL userSetsEqual = [[userSet1 valueForKey:@"itemProperties"]
isEqualToSet:[userSet2 valueForKey:@"itemProperties"]];
... where userSet1 and userSet2 were the NSSets that contained User objects.
...其中userSet1和userSet2是包含User对象的NSSets。
#3
0
Looking through the documentation, it seems that there is no way to really handle this special case of yours. You're going to have to write some custom code to handle this. Personally, I would recommend using -sortedArrayUsingDescriptors:
and then comparing the arrays, but that's just me. You could also go enumerate through one set, then narrow down the other using -filteredSetUsingPredicate:
and get its count
.
通过文档查看,似乎没有办法真正处理你的这种特殊情况。你将不得不编写一些自定义代码来处理这个问题。就个人而言,我建议使用-sortedArrayUsingDescriptors:然后比较数组,但那只是我。您也可以通过一组进行枚举,然后使用-filteredSetUsingPredicate缩小另一组:并获取其计数。
Whichever method you use, consider the fact that its probably not going to be super efficient. This might be unavoidable, but there are probably ways to go about it that are better than others. Food for thought.
无论您使用哪种方法,都要考虑它可能不会超高效的事实。这可能是不可避免的,但有可能采取比其他方法更好的方法。值得深思。
#1
2
You could just call valueForKey:
on both sets and compare the results.
您可以在两个集合上调用valueForKey:并比较结果。
if ([[set1 valueForKey:@"name"] isEqualToSet:[set2 valueForKey:@"name"]]) {
// the sets match your criteria
}
#2
3
I had the same problem, but I needed to compare multiple properties at the same time (class User with properties Name and Id).
我有同样的问题,但我需要同时比较多个属性(类User与属性Name和Id)。
I resolved this by adding a method returning an NSDictionary with the properties needed to the class:
我通过添加一个返回NSDictionary的方法来解决这个问题,该方法具有该类所需的属性:
- (NSDictionary *)itemProperties
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:self.name forKey:@"name"];
[dict setObject:self.id forKey:@"id"];
return dict;
}
and then using valueForKey:
as Kevin Ballard mentioned:
然后使用valueForKey:正如Kevin Ballard所说:
BOOL userSetsEqual = [[userSet1 valueForKey:@"itemProperties"]
isEqualToSet:[userSet2 valueForKey:@"itemProperties"]];
... where userSet1 and userSet2 were the NSSets that contained User objects.
...其中userSet1和userSet2是包含User对象的NSSets。
#3
0
Looking through the documentation, it seems that there is no way to really handle this special case of yours. You're going to have to write some custom code to handle this. Personally, I would recommend using -sortedArrayUsingDescriptors:
and then comparing the arrays, but that's just me. You could also go enumerate through one set, then narrow down the other using -filteredSetUsingPredicate:
and get its count
.
通过文档查看,似乎没有办法真正处理你的这种特殊情况。你将不得不编写一些自定义代码来处理这个问题。就个人而言,我建议使用-sortedArrayUsingDescriptors:然后比较数组,但那只是我。您也可以通过一组进行枚举,然后使用-filteredSetUsingPredicate缩小另一组:并获取其计数。
Whichever method you use, consider the fact that its probably not going to be super efficient. This might be unavoidable, but there are probably ways to go about it that are better than others. Food for thought.
无论您使用哪种方法,都要考虑它可能不会超高效的事实。这可能是不可避免的,但有可能采取比其他方法更好的方法。值得深思。