文章40 | 阅读 19078 | 点赞0
- (NSUInteger)hash {
// 假设对象有三个属性,那么对这三个属性分别算出 hash 值,然后进行异或运算
return [self.property1 hash] ^ [self.property2 hash] ^ [self.property3 hash];
}
- (NSUInteger)hash {
NSUInteger subhashes[] = {[self.property1 hash], [self.property2 hash], [self.property3 hash]};
NSUInteger result = subhashes[0];
for (int ii = 1; ii < 3; ++ii) {
unsigned long long base = (((unsigned long long)result) << 32 | subhashes[ii]);
base = (~base) + (base << 18);
base ^= (base >> 31);
base *= 21;
base ^= (base >> 11);
base += (base << 6);
base ^= (base >> 22);
result = base;
}
return result;
}
@interface Person : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, strong) NSDate *birthday;
@end
- (BOOL)isEqual:(id)object {
if (self == object) {
return YES;
}
if (![object isKindOfClass:[Person class]]) {
return NO;
}
return [self isEqualToPerson:(Person *)object];
}
- (BOOL)isEqualToPerson:(Person *)person {
if (!person) {
return NO;
}
BOOL haveEqualNames = (!self.name && !person.name) || [self.name isEqualToString:person.name];
BOOL haveEqualBirthdays = (!self.birthday && !person.birthday) || [self.birthday isEqualToDate:person.birthday];
return haveEqualNames && haveEqualBirthdays;
}
- (BOOL)isEqual:(PersonModel *)object {
if (self == object) {
return YES;
}
else if (self == nil || object == nil || ![object isKindOfClass:[self class]]) {
return NO;
}
return
(_property1 == object->_property1 ? YES : [_property1 isEqual:object->_property1]) &&
(_property2 == object->_property2 ? YES : [_property2 isEqual:object->_property2]) &&
(_property3 == object->_property3 ? YES : [_property3 isEqual:object->_property3]);
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/Forever_wj/article/details/120091926
内容来源于网络,如有侵权,请联系作者删除!