Comparing two NSDictionaries and Find Difference

swift compare dictionaries
swift dictionary diff

I am working on an iOS app, where I will be getting a JSON Object from server, which will be populated on a UITableView.

User can change values on tableview, Hence resulting in a new JSON. Now I want to send only delta (Difference of Two JSON Objects) back to server. I know I can traverse both Objects for finding delta. But just wish to know is there any easy solution for this problem.

Ex:

NSDictionary *dict1 = {@"Name" : "John", @"Deptt" : @"IT"};
NSDictionary *dict2 = {@"Name" : "Mary", @"Deptt" : @"IT"};

Delta = {@"Name" : "Mary"}

Considering new value is Mary for key name;

Thanks In Advance

isEqualToDictionary: Returns a Boolean value that indicates whether the contents of the receiving dictionary are equal to the contents of another given dictionary.

if ([NSDictionary1 isEqualToDictionary:NSDictionary2) {
   NSLog(@"The two dictionaries are equal.");
}

Two dictionaries have equal contents if they each hold the same number of entries and, for a given key, the corresponding value objects in each dictionary satisfy the isEqual: test.

Comparing two NSDictionaries and Find Difference - ios, Now I want to send only delta (Difference of Two JSON Objects) back to server. I know I can traverse both Objects for finding delta. But just wish to know is there  I have two NSDictionaries containing NSStrings.To compare this two dictionaries I use isEqualToDictionary: method. The documentation on isEqualToDictionary:says "Two dictionaries have equal contents if they each hold the same number of entries and, for a given key, the corresponding value objects in each dictionary satisfy the isEqual: test."

Here's how to get all the keys with non-matching values. What to do with those keys is app level question, but the most informative structure would include an array of mismatched values from both dictionaries, as well has handle keys from one that are not present in the other:

NSMutableDictionary *result = [@{} mutableCopy];

// notice that this will neglect keys in dict2 which are not in dict1
for (NSString *key in [dict1 allKeys]) {
    id value1 = dict1[key];
    id value2 = dict2[key];
    if (![value1 equals:value2]) {
        // since the values might be mismatched because value2 is nil
        value2 = (value2)? value2 : [NSNull null];
        result[key] = @[value1, value2];
    }
}

// for keys in dict2 that we didn't check because they're not in dict1
NSMutableSet *set1 = [NSMutableSet setWithArray:[dict1 allKeys]];
NSMutableSet *set2 = [NSMutableSet setWithArray:[dict2 allKeys]];

for (NSString *key in [set2 minusSet:set1]) {
    result[key] = @[[NSNull null], dict2[key]];
}

There are certainly more economical ways to do it, but this code is optimized for instruction.

isEqual(to:), Two dictionaries have equal contents if they each hold the same number of entries and, for a given key, the corresponding value objects in each dictionary  SO let’s start learning how to compare two dictionaries in Python and find similarities between them. Similarities between dictionaries in Python. Basically A dictionary is a mapping between a set of keys and values. The keys support the basic operations like unions, intersections, and differences.

Just enumerate through and compare the dictionaries key-by-key. This will output any differences as well as any unmatched keys on either side, you can tweak the logic depending on exactly what you want to include.

- (NSDictionary *)delta:(NSDictionary *)dictionary
{
    NSMutableDictionary *result = NSMutableDictionary.dictionary;

    // Find objects in self that don't exist or are different in the other dictionary
    [self enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        id otherObj = dictionary[key];

        if (![obj isEqual:otherObj]) {
            result[key] = obj;
        }
    }];

    // Find objects in the other dictionary that don't exist in self
    [dictionary enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        id selfObj = self[key];

        if (!selfObj) {
            result[key] = obj;
        }
    }];

    return result;
}

Key difference between Dictionary and NSDictionary, As a result, two String instances can be equal even if they contain different Unicode code units. An NSDictionary uses the Objective-C method -isEqual: to compare its keys, and And that's what you get with object(forKey:) . Diffchecker is a diff tool to compare text differences between two text files. Enter the contents of two files and click Find Difference. Try our desktop app. Offline mode, advanced features and more. Check out Diffchecker Desktop. Diffchecker Desktop Run Diffchecker offline, on your computer, with more features! 30-day free trial.

Beginning iOS Game Development, You pass in the scalar C value, and you get back an NSNumber object that If the two pointers are pointing to the same object, the == comparison will be true. mathematically equal but are two different objects, the comparison will return false. framework that you can use to manage collections: NSSet and NSDictionary. Use Database Compare to find design changes between two Access desktop database files or two versions of the same file. Database Compare alerts you to additions, deletions, or renaming of objects such as tables, queries, forms, reports, and VBA modules.

Equality, When you use the == operator to compare two objects in Objective-C, you're NSObject and NSValue have different semantics for equality, and You'll find plenty of other value types throughout Foundation — just look For container classes like NSArray and NSDictionary , deep equality comparison is  Percentage difference is usually calculated when you want to know the difference in percentage between two numbers. For this calculator, the order of the numbers does not matter as we are simply dividing the difference between two numbers by the average of the two numbers. To calculate the difference relatively,

Compare two NSDictionaries · GitHub, -(BOOL)isEtalonDictionary:(NSDictionary *)etalonDictionary sameAs:(​NSDictionary *)dictionary{. BOOL res = NO;. for (id item in etalonDictionary){. NSObject  Text Compare! is an online diff tool that can find the difference between two text documents. Just paste and compare.

Comments
  • How do you take Mary as the value when compared to john ?
  • Check this link under comparing dictionaries. If your dictionaries are identical in terms of the keys, you could just do a for loop and compare values of the keys in dict1 to dict2.
  • This answers nothing. @Yukta asked for delta, not boolean if delta exists.