Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the identifier property to compare objects if it exists #1

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions SMModelObject.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ + (void) initialize {

for (Class cls = self; cls != [SMModelObject class]; cls = [cls superclass]) {

unsigned int varCount;
Ivar *vars = class_copyIvarList(cls, &varCount);
unsigned int count;
objc_property_t *properties = class_copyPropertyList(cls, &count);

for (int i = 0; i < varCount; i++) {
Ivar var = vars[i];
NSString *name = [[NSString alloc] initWithUTF8String:ivar_getName(var)];
for (unsigned int i = 0; i < count; i++) {
objc_property_t property = properties[i];
NSString *name = [[NSString alloc] initWithUTF8String:property_getName(property)];
[names addObject:name];
}

free(vars);
free(properties);
}

[keyNames setObject:names forKey:self];
}

- (NSArray *)allKeys {
- (NSArray *) allKeys {
return [keyNames objectForKey:[self class]];
}

Expand Down Expand Up @@ -74,6 +74,9 @@ - (BOOL) isEqual:(id)other {

if ([other isKindOfClass:[self class]]) {

if ([[self allKeys] containsObject:@"identifier"])
return [[self valueForKey:@"identifier"] isEqual:[other valueForKey:@"identifier"]];

for (NSString *name in [self allKeys]) {
id a = [self valueForKey:name];
id b = [other valueForKey:name];
Expand All @@ -90,13 +93,16 @@ - (BOOL) isEqual:(id)other {

// Must override hash as well, this is taken directly from RMModelObject, basically
// classes with the same layout return the same number.
- (NSUInteger)hash {
return (NSUInteger)[self allKeys];
- (NSUInteger) hash {
if ([[self allKeys] containsObject:@"identifier"])
return [[self valueForKey:@"identifier"] hash];
else
return (NSUInteger)[self allKeys];
}

- (void)writeLineBreakToString:(NSMutableString *)string withTabs:(NSUInteger)tabCount {
- (void) writeLineBreakToString:(NSMutableString *)string withTabs:(NSUInteger)tabCount {
[string appendString:@"\n"];
for (int i=0;i<tabCount;i++) [string appendString:@"\t"];
for (NSUInteger i=0;i<tabCount;i++) [string appendString:@"\t"];
}

// Prints description in a nicely-formatted and indented manner.
Expand Down