Skip to content

Commit 2dc5187

Browse files
author
Ricardo Pramana Suranta
committed
Add custom equality and extra copy logic for JSONAPIResource
2 parents 3d5ac61 + f3bda62 commit 2dc5187

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Classes/JSONAPIResource.m

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,15 @@ - (id)copyWithZone:(NSZone *)zone {
219219

220220
}
221221

222+
BOOL isEmptyDictionary = !self.__dictionary || self.__dictionary.count == 0;
223+
224+
if (isEmptyDictionary) {
225+
for (NSString *key in self.propertyKeys) {
226+
id selfValue = [self valueForKey:key];
227+
[copy setValue:selfValue forKey:key];
228+
}
229+
}
230+
222231
return copy;
223232
}
224233

@@ -295,4 +304,32 @@ - (void)encodeWithCoder:(NSCoder *)aCoder
295304
}
296305
}
297306

307+
#pragma mark - NSObject -
308+
309+
- (BOOL)isEqual:(id)object
310+
{
311+
if (self == object) return YES;
312+
if (![object isMemberOfClass:[self class]]) return NO;
313+
314+
for (NSString *key in self.propertyKeys) {
315+
id selfValue = [self valueForKey:key];
316+
id objectValue = [object valueForKey:key];
317+
318+
BOOL valuesEqual = ((selfValue == nil && objectValue == nil) || [selfValue isEqual:objectValue]);
319+
if (!valuesEqual) return NO;
320+
}
321+
322+
return YES;
323+
}
324+
325+
- (NSUInteger)hash {
326+
NSUInteger value = 0;
327+
328+
for (NSString *key in self.propertyKeys) {
329+
value ^= [[self valueForKey:key] hash];
330+
}
331+
332+
return value;
333+
}
334+
298335
@end

0 commit comments

Comments
 (0)