我有一个叫做的课
Contact;
Run Code Online (Sandbox Code Playgroud)
在Contact我有(simple version to test, no hash yet)
- (BOOL)isEqual:(id)other {
if (other == self)
return YES;
if (!other || ![other isKindOfClass:[self class]])
return NO;
return NO;
}
Run Code Online (Sandbox Code Playgroud)
我做:
NSMutableArray *arr = [[NSMutableArray alloc] init];
Contact *contact = [Contact new];
[arr addObject:contact]
// I expect my isEqual to be called here but it does not get called
[arr containsObject:contact] // this evaluates to true somehow!!!
Run Code Online (Sandbox Code Playgroud)
但是,如果我添加另一个要键入的对象NSString,则会调用它来比较String对象而不是联系对象.意思是
[arr addObject:@""] // so now arr has two elements
// here I expect two calls to isEqual but only one gets there
// when comparing string object against Contact
[arr containsObject:contact]
Run Code Online (Sandbox Code Playgroud)
为什么isEqual在我上面提到的情况下没有被调用?
请阅读参考库中isEqual: NSObject协议中的讨论.
您会发现,对于集合内部的对象(例如NSArray),hash可能会用于确定两个对象是否实际相同.如果两个指针实际指向同一个对象,则无需检查是否相等 - 因此isEqual:永远不会被调用.
参考库建议的解决方案也是hash在子类中实现的.
| 归档时间: |
|
| 查看次数: |
643 次 |
| 最近记录: |