Размещение рабочего кода здесь на всякий случай, если это поможет другим - этот код учитывает, что один или оба объекта являются нулевыми (что не будет сравниваться при использовании isEqual):
синтезирует propertyNames в объектной модели:
инициализировать один раз массивом массив строк с ProperyNames:
-(void)initPropertyNamesArray {
propertyNames = [[NSArray alloc] initWithObjects:
@"lastName",
@"firstName",
@"dob",
@"notes", ..., nil];
}
Переопределить метод isEqual:
(в этом примере сравниваются типы объектов, которые я использую в своей модели - строки, даты и изображения):
- (BOOL) isEqual:(Patient *)otherPatient {
if (!self.propertyNames) [self initPropertyNamesArray];
BOOL showLog = YES;
if (showLog) NSLog(@"Patient ISEQUAL");
for(NSString *property in self.propertyNames) {
id obj1 = [self valueForKey:property];
id obj2 = [otherPatient valueForKey:property];
if (showLog) NSLog(@"Comparing %@:", property);
//CHECK IF ONE OR THE OTHER IS NULL -- the're not equal if so
if (! (obj1 && obj2)) {
if ((obj1 && !obj2) || (!obj1 && obj2)) {
if (showLog) NSLog(@"%@ is null %@ is not!", obj1?@"TO:":@"FROM:", obj2?@"TO:":@"FROM:");
return NO;
}
}
//STRING
if([obj1 isKindOfClass:[NSString class]]) {
if(![obj2 isKindOfClass:[NSString class]])
return NO;
NSString * strObj1 = (NSString *)obj1;
NSString * strObj2 = (NSString *)obj2;
if (strObj1 && strObj2) {
if (![strObj1 isEqualToString:strObj2]) {
if (showLog) NSLog(@"%@ changed from:%@ to:%@", property, strObj1, strObj2);
return NO;
}
} else if (! (!strObj1 && !strObj2)) { //one is nil one isn't NOT EQUAL
if (showLog) NSLog(@"%@ changed from:%@ to:%@", property, strObj1, strObj2);
return NO;
}
//both nil (call them equal)
//DATE
} else if([obj1 isKindOfClass:[NSDate class]]) {
if(![obj2 isKindOfClass:[NSDate class]])
return NO;
NSDate * dateObj1 = (NSDate *)obj1;
NSDate * dateObj2 = (NSDate *)obj2;
if (dateObj1 && dateObj2) {
if (![dateObj1 isEqualToDate:dateObj2]) {
if (showLog) NSLog(@"%@ changed from:%@ to:%@", property, dateObj1, dateObj2);
return NO;
}
} else if (! (!dateObj1 && !dateObj2)) { //one is nil one isn't NOT EQUAL
if (showLog) NSLog(@"%@ changed from:%@ to:%@", property, dateObj1, dateObj2);
return NO;
}
//both nil (call them equal)
//IMAGE
} else if([obj1 isKindOfClass:[UIImage class]] && ![property isEqual:@"lastUpdated"]) {
if(![obj2 isKindOfClass:[UIImage class]])
return NO;
UIImage * imgObj1 = (UIImage *)obj1;
UIImage * imgObj2 = (UIImage *)obj2;
if (imgObj1 && imgObj2) { //check both not nil
if(![imgObj1 isEqual:imgObj2]) {
if (showLog) NSLog(@"%@ changed from:%@ to:%@", property, imgObj1, imgObj2);
return NO;
}
} else if (! (!imgObj1 && !imgObj2)) { //one is nil one isn't NOT EQUAL
if (showLog) NSLog(@"%@ changed from:%@ to:%@", property, imgObj1, imgObj2);
return NO;
}
//both nil (call them equal)
}
if (showLog) NSLog(@"OK!");
}
if (showLog) NSLog(@"PATIENTS ARE THE SAME!");
return YES;
}
Вы, очевидно, можете убрать все NSLogs, которые я вставил для отладки.
Спасибо всем, кто помог мне!