大佬教程收集整理的这篇文章主要介绍了ios – 比较两个NSDictionaries和Find Difference,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
用户可以在tableview上更改值,从而生成新的JSON.
现在我想只将delta(两个JSON对象的差异)发送回服务器.
我知道我可以遍历两个对象来寻找delta.但只是想知道这个问题是否有任何简单的解决方案.
例如:
NSDictionary *Dict1 = {@"Name" : "John",@"Deptt" : @"IT"}; NSDictionary *Dict2 = {@"Name" : "Mary",@"Deptt" : @"IT"}; Delta = {@"Name" : "Mary"}
提前致谢
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]]; }
以上是大佬教程为你收集整理的ios – 比较两个NSDictionaries和Find Difference全部内容,希望文章能够帮你解决ios – 比较两个NSDictionaries和Find Difference所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。