【问题标题】:Comparing two NSDictionaries and Find Difference比较两个 NSDictionaries 并找出差异
【发布时间】:2016-07-14 13:25:37
【问题描述】:

我正在开发一个 iOS 应用程序,我将从服务器获取 JSON 对象,该对象将填充到 UITableView 上。

用户可以更改 tableview 上的值,从而产生一个新的 JSON。 现在我只想将 delta(两个 JSON 对象的差异)发送回服务器。 我知道我可以遍历两个对象来查找增量。但只是想知道这个问题有没有简单的解决方案。

例如:

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

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

考虑新值是 Mary 作为键名;

提前致谢

【问题讨论】:

  • 与 john 相比,你如何看待 Mary 作为价值?
  • 在比较字典下查看this link。如果您的字典在键方面是相同的,您可以只执行一个 for 循环并比较 dict1dict2 中键的值。

标签: ios objective-c uitableview nsdictionary


【解决方案1】:

isEqualToDictionary: 返回一个布尔值,指示接收字典的内容是否等于另一个给定字典的内容。

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

如果两个字典都包含相同数量的条目,则它们具有相同的内容,并且对于给定的键,每个字典中的相应值对象满足 isEqual: 测试。

【讨论】:

  • 这没有任何答案。 @Yukta 要求 delta,如果 delta 存在,则不是布尔值。
  • 是的,它确实回答了......但不是完整的问题。如果这返回 NO - 无需更进一步。此外,这是解决(即使是间接地)深度比较问题的唯一答案(因为 JSON 和 NSDictionaries 通常表示对象的层次结构)。应用于每对值的 'isEqual:' - 非常重要。
【解决方案2】:

以下是获取所有值不匹配的键的方法。如何处理这些键是应用程序级别的问题,但信息最丰富的结构将包括两个字典中不匹配的值的数组,以及处理另一个字典中不存在的键:

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]];
[set2 minusSet:set1]
for (NSString *key in set2) {
    result[key] = @[[NSNull null], dict2[key]];
}

当然有更经济的方法可以做到这一点,但这段代码针对指令进行了优化。

【讨论】:

    【解决方案3】:

    只需逐个关键字枚举和比较字典即可。这将输出任何差异以及任何一侧的任何不匹配的键,您可以根据您想要包含的内容调整逻辑。

    - (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;
    }
    

    【讨论】:

      猜你喜欢
      • 2017-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-19
      • 1970-01-01
      • 2011-06-24
      • 1970-01-01
      相关资源
      最近更新 更多