Skip to content

Instantly share code, notes, and snippets.

@erikkerber
Last active December 30, 2015 10:39
Show Gist options
  • Save erikkerber/7817292 to your computer and use it in GitHub Desktop.
Save erikkerber/7817292 to your computer and use it in GitHub Desktop.
#import <objc/runtime.h>
- (BOOL) objectsAreEqual:(NSObject *)o1 objectTwo:(NSObject *)o2 {
if([o1 class] != [o2 class]) return NO;
NSUInteger property_count;
objc_property_t * property_list = class_copyPropertyList([o1 class], &property_count); // Must Free, later
for (int i = 0; i <= property_count ; i++) { // Reverse order, to get Properties in order they were defined
objc_property_t property = property_list[i];
// For Eeach property we are loading its name
const char * property_name = property_getName(property);
NSString * propertyName = [NSString stringWithCString:property_name encoding:NSASCIIStringEncoding];
if (propertyName) { // and if name is ok, we are getting value using KVC
id value1 = [o1 valueForKey:propertyName];
id value2 = [o2 valueForKey:propertyName];
// TODO - Too lazy to figure out all equality cases
if([o1 class] == [NSString class] &&
![(NSString *)o1 isEqualToString:(NSString *)o2])
{
return NO;
}
else if(o1 != o2)
{
return NO;
}
}
}
free(property_list);//Clean up
return YES;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment