Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSMutableDictionary addEntriesFromDictionary未正确合并词典_Ios_Objective C_Json_Nsdictionary_Nsmutabledictionary - Fatal编程技术网

Ios NSMutableDictionary addEntriesFromDictionary未正确合并词典

Ios NSMutableDictionary addEntriesFromDictionary未正确合并词典,ios,objective-c,json,nsdictionary,nsmutabledictionary,Ios,Objective C,Json,Nsdictionary,Nsmutabledictionary,我有一个JSON响应,存储为NSMutableDictionary,如下所示: { "list": { "ID1", "ID2", "ID3" }, "items": { "ID1" : { "name" : "shoe" }, "ID2" : { "name" : "pants" }, "ID3" : { "name" : "hat" } } } 我需要让

我有一个JSON响应,存储为NSMutableDictionary,如下所示:

    {
        "list": { "ID1", "ID2", "ID3" },

        "items": {
            "ID1" : { "name" : "shoe" },
            "ID2" : { "name" : "pants" },
            "ID3" : { "name" : "hat" }
        }
    }
我需要让NSMutableDictionary添加来自任何其他JSON响应的条目,因此如果我收到如下新响应:

{
    "list": { "ID4", "ID5", "ID6" },

    "items": {
        "ID4" : { "name" : "shirt" },
        "ID5" : { "name" : "tie" },
        "ID6" : { "name" : "glasses" }
    }
}
    {
        "list": { "ID1", "ID2", "ID3", "ID4", "ID5", "ID6" },

        "items": {
            "ID1" : { "name" : "shoe" },
            "ID2" : { "name" : "pants" },
            "ID3" : { "name" : "hat" },
            "ID4" : { "name" : "shirt" },
            "ID5" : { "name" : "tie" },
            "ID6" : { "name" : "glasses" }
        }
    }
更新的NSMutableDictionary需要如下显示:

{
    "list": { "ID4", "ID5", "ID6" },

    "items": {
        "ID4" : { "name" : "shirt" },
        "ID5" : { "name" : "tie" },
        "ID6" : { "name" : "glasses" }
    }
}
    {
        "list": { "ID1", "ID2", "ID3", "ID4", "ID5", "ID6" },

        "items": {
            "ID1" : { "name" : "shoe" },
            "ID2" : { "name" : "pants" },
            "ID3" : { "name" : "hat" },
            "ID4" : { "name" : "shirt" },
            "ID5" : { "name" : "tie" },
            "ID6" : { "name" : "glasses" }
        }
    }
不幸的是,当我调用带有附加项的
addEntriesFromDictionary
时,我得到以下结果:

{
            "list": { "ID1", "ID2", "ID3" },

            "items": {
                "ID1" : { "name" : "shoe" },
                "ID2" : { "name" : "pants" },
                "ID3" : { "name" : "hat" }
            }
        }
             "list": { "ID4", "ID5", "ID6" },

             "items": {
                 "ID4" : { "name" : "shirt" },
                 "ID5" : { "name" : "tie" },
                 "ID6" : { "name" : "glasses" }
           }
    }

假设我们拥有与您示例中相同的词典:

let key1 = "list"
let key2 = "items"

var rec = [key1:["ID1","ID2","ID3"],
           key2:["ID1":["name":"shoe"],"ID2":["name":"pants"],"ID3":["name":"hat"]]] as [String : Any]
let inc = [key1:["ID4","ID5","ID6"],
           key2:["ID4":["name":"shirt"],"ID5":["name":"tie"],"ID6":["name":"glasses"]]] as [String : Any]
我使用以下基本原理来找到解决方案:

。。。在下文中实现了此代码段:

func merge(_ inc:[String:Any], into rec: inout [String:Any]) -> [String:Any] {
    for (_, vals) in inc {
        if var recKeys = rec[key1] as? [String],
            var recItems = rec[key2] as? [String:[String:String]],
            let incItems = inc[key2] as? [String:[String:String]] {

            if let incValIds = vals as? [String] {
                for id in incValIds {
                    if let newVal = incItems[id] {
                        if recKeys.contains(id) {
                            for (newValId, newValObj) in newVal {
                                guard var tab = recItems[id] else { continue }
                                tab[newValId] = newValObj
                                recItems[id] = tab
                            }
                        } else {
                            recKeys.append(id)
                            recItems[id] = newVal
                        }
                    }
                }
            }
            rec[key1] = recKeys
            rec[key2] = recItems
        }
    }
    return rec
}
。。。并使用如下所述的函数来获得以下定义的结果:

let updatedInfo = merge(inc, into: &rec)
print(updatedInfo)


您现在可以根据需要正确地合并提供的两个词典。

我认为您需要做的是所谓的“深度合并”,这可能会对您有所帮助:我建议您创建一个类,该类使用适当的初始值设定项和函数来表示数据,以接受字典,而不是直接依赖字典结构