Skip to content

Commit a51bf2e

Browse files
author
Ricardo Pramana Suranta
committed
Implement custom equality on JSONAPIResource class
1 parent 3d5ac61 commit a51bf2e

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Classes/JSONAPIResource.m

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,32 @@ - (void)encodeWithCoder:(NSCoder *)aCoder
295295
}
296296
}
297297

298+
#pragma mark - NSObject -
299+
300+
- (BOOL)isEqual:(id)object
301+
{
302+
if (self == object) return YES;
303+
if (![object isMemberOfClass:[self class]]) return NO;
304+
305+
for (NSString *key in self.propertyKeys) {
306+
id selfValue = [self valueForKey:key];
307+
id objectValue = [object valueForKey:key];
308+
309+
BOOL valuesEqual = ((selfValue == nil && objectValue == nil) || [selfValue isEqual:objectValue]);
310+
if (!valuesEqual) return NO;
311+
}
312+
313+
return YES;
314+
}
315+
316+
- (NSUInteger)hash {
317+
NSUInteger value = 0;
318+
319+
for (NSString *key in self.propertyKeys) {
320+
value ^= [[self valueForKey:key] hash];
321+
}
322+
323+
return value;
324+
}
325+
298326
@end

0 commit comments

Comments
 (0)