从两个不同的字典数组中删除共同值

时间:2019-06-17 12:31:50

标签: swift4.2

我有2个字典数组:

serverContactsDict = [["name": “Miya”,”number": “4178547852”],
                      [“name": “Simon”,”number": “2458741458”],
                      ["name": “Pio”,”number": “7414785236”]]

phoneContactsDict = [["name": “Miya”,”number": “4178547852”],
                     [“name": “Simon”,”number": “2458741458”],
                     ["name": “Pio”,”number": “7414785236”],
                     ["name": “Kayle”,”number": “2547859632”],
                     ["name": “Hoya”,”number": “7485965841”]]

DesiredDict = [["name": “Kayle”,”number": “2547859632”],
               ["name": “Hoya”,”number": “7485965841”]]

尝试了很多,但没有得到想要的结果。

1 个答案:

答案 0 :(得分:0)

let desiredDict = phoneContactsDict.filter {
  let dict = $0
  return !serverContactsDict.contains{ dict == $0 }
}