检查字典是否在Swift 3中的字典数组中

jay*_*ixz 3 arrays dictionary contains swift

我有一系列这样的词典:

var suggestions = [["keyword": "apple", "identifier": "0"], ["keyword": "banana", "identifier": "1"], ["keyword": "carrot", "identifier": "2"]]
Run Code Online (Sandbox Code Playgroud)

我想追加建议数组,在我这样做之前,我想知道字典是否已经存在于我的数组中以防止重复.我怎么能在Swift 3中做到这一点?

我试图使用contains(where: ([String: String)])Swift 3 的功能,但我似乎无法使其工作.

更新:丹尼尔霍尔的答案使其成功.这是Swift 3的确切代码:

let newDictionary = ["keyword": "celery", "identifier": "3"]
if !suggestions.contains(where: {$0 == newDictionary}) {
    suggestions.append(newDictionary)
}
Run Code Online (Sandbox Code Playgroud)

Dan*_*all 7

我认为解决方案比其他答案建议的更直接.只需使用:

let newDictionary = ["keyword":"celery", "identifier": "3"]
if !suggestions.contains{ $0 == newDictionary } {
    suggestions.append(newDictionary)
}
Run Code Online (Sandbox Code Playgroud)

这样可以确保现有的词典数组在添加之前不包含要添加的新词典.