在Swift中搜索字符串数组的字典

Dan*_*iel 2 dictionary functional-programming swift

我想在映射到字符串数组的int字典中查找字符串值.我可以使用下面的代码来处理映射到字符串但不是字符串数组的整数字典.我怎样才能做到这一点?我尝试在我的过滤器中嵌套另一个过滤器,但没有运气.

let dict = [1 : ["one", "two"], 2 : ["red", "green"], 3 : ["World", "are", "you"]]

// Doesn't work for array of strings.
let keys = dict.filter {
    return $0.1.containsString("are")  // Should return 3
}.map {
    return $0.0
}
Run Code Online (Sandbox Code Playgroud)

Luk*_*itz 5

contains改用.

let keys = dict
  .filter {$0.1.contains("are")}
  .map { $0.0 }
Run Code Online (Sandbox Code Playgroud)

这返回3.