【问题标题】:NSMutableDictionary addEntriesFromDictionary not merging dictionaries properlyNSMutableDictionary addEntriesFromDictionary 没有正确合并字典
【发布时间】:2017-05-25 12:34:41
【问题描述】:

我有一个 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" }
    }
}

更新后的 NSMutableDictionary 需要如下所示:

    {
        "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" }
           }
    }

【问题讨论】:

标签: ios objective-c json nsdictionary nsmutabledictionary


【解决方案1】:

假设我们有与您的示例相同的字典:

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]

我使用以下基本原理来寻找解决方案:

...以后实现了这个代码sn-p:

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)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-22
    • 2019-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-28
    • 2012-01-03
    相关资源
    最近更新 更多