NSSet -member检查NSValue的相等性 [英] NSSet -member to check equality of NSValue
问题描述
我有一个 NSSet
,包含数千个 NSValue
对象(包装 CGPoints
)。我想很快找到 NSSet
中是否存在给定的 CGPoint
值。在我看来, NSSet
的成员:
方法可以在这里完成工作,除了它检查是否相等使用 isEqual:
。 NSValue
对象使用 isEqualToValue:
,所以当我执行代码时:
I have a NSSet
containing many thousands of NSValue
objects (wrapping CGPoints
). I would like to very quickly find if a given CGPoint
value exists in the NSSet
. It seems to me that the member:
method of an NSSet
might do the job here, except that it checks for equality using isEqual:
. NSValue
objects use isEqualToValue:
, and so when I execute the code:
[mySet member:valueToCheck];
它实际上导致Xcode崩溃。
it actually causes Xcode to crash.
1)是否有某种方法可以使用自定义相等检查使其适用于 NSValue
对象?
1) Is there some way to use a custom equality check to make this work for NSValue
objects?
2 )这是否是最好的方法(即首先是成员:
足够快)?场景是我有一个 NSSet
,其中包含大量代表屏幕(iPad)像素的点。后来我需要以每秒数千点的速度轰击那一组,看看它们是否存在于集合中。我的方法似乎很难实现这一点。我想创建类似于一个巨大的二维位数组的东西,每个索引代表一个屏幕上的像素。一旦我知道我正在测试的那一点,我就可以直接跳到阵列中的那一点并检查1或0 ......这听起来好不好?
2) Is this even the best approach (i.e. is member:
quick enough in the first place)? The scenario is that I have a NSSet
containing a large number of points representing pixels on the screen (iPad). Later on I need to bombard that set with many thousands of points per second to see if they exist in the set. My approach seems crude for achieving this. I thought about creating something like a huge 2-dimensional bit array, with each index representing a pixel on screen. Once I know the point I'm testing for, I can just jump straight to that point in the array and check for a 1 or 0... does this sound better or worse?
谢谢
推荐答案
你能把它变成一个简单的可重复的案例吗?例如,我刚试过:
Can you get this to a simple reproducible case? For example, I just tried:
NSValue *v = [NSValue valueWithCGPoint:CGPointMake(1, 1)];
NSSet *s = [NSSet setWithObject:v];
NSLog(@"%@", [s member:[NSValue valueWithCGPoint:CGPointMake(1, 1)]]);
但它运作正常。
编辑
-isEqual:
不是问题:
NSValue *v1 = [NSValue valueWithPoint:NSMakePoint(1, 1)];
NSValue *v2 = [NSValue valueWithPoint:NSMakePoint(1, 1)];
NSLog(@"%d", [v1 isEqual:v2]); //logs "1"
-hash
不是问题:
NSLog(@"%d", ([v1 hash] == [v2 hash])); //logs "1"
它们是不同的对象:
NSLog(@"%d", (v1 != v2)); //logs "1"
问题在于您的代码。尝试清理和重建。
The problem is in your code. Try cleaning and rebuilding.
这篇关于NSSet -member检查NSValue的相等性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!