Skip to content

Instantly share code, notes, and snippets.

@lzwjava
Created November 8, 2015 11:57
Show Gist options
  • Save lzwjava/dc269b7c079209d0c5d2 to your computer and use it in GitHub Desktop.
Save lzwjava/dc269b7c079209d0c5d2 to your computer and use it in GitHub Desktop.
#pragma mark - Match tool
- (BOOL)miniDictionary:(NSDictionary *)input matchTo:(NSDictionary *)dict {
if (input.count == 0 || dict.count == 0) {
return YES;
}
for (NSString *key in [input allKeys]) {
id inputValue = input[key];
id actualValue = dict[key];
if (![self mini:inputValue matchTo:actualValue]) {
return NO;
}
}
return YES;
}
- (BOOL)miniArray:(NSArray *)input matchTo:(NSArray *)array {
if (input.count == 0 || array.count == 0) {
return YES;
}
if (input.count != array.count) {
return NO;
}
for (id object in input) {
NSInteger index = [input indexOfObject:object];
id actual = array[index];
if (![self mini:object matchTo:actual]) {
return NO;
}
}
return YES;
}
/// parameters' order is unimportant
- (BOOL)mini:(id)input matchTo:(id)object {
if (input == nil || object == nil) {
NSLog(@"one of %@, %@ is nil", input, object);
return YES;
}
BOOL result = NO;
if ([input isKindOfClass:[NSDictionary class]] && [object isKindOfClass:[NSDictionary class]]) {
return [self miniDictionary:input matchTo:object];
} else if ([input isKindOfClass:[NSArray class]] && [object isKindOfClass:[NSArray class]]) {
return [self miniArray:input matchTo:object];
} else if ([input isKindOfClass:[NSString class]] && [object isKindOfClass:[NSString class]]) {
result = [input isEqualToString:object];
} else if ([input isKindOfClass:[NSNumber class]] && [object isKindOfClass:[NSNumber class]]) {
result = [input isEqualToNumber:object];
} else {
@try {
result = [input isEqualToValue:object];
}
@catch (NSException *exception) {
NSLog(@"catch exception %@ when compare %@ to %@", exception, input, object);
}
}
if (!result) {
NSLog(@"%@ isn't equal to %@", input, object);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment