[英]Swift: Cannot convert value of type '(Any) throws -> Bool? to expected argument type '(Any) throws -> Bool?
I'm trying to take two arrays and combine them into a new array without any duplicates.我正在尝试使用两个数组并将它们组合成一个没有任何重复的新数组。 I can't get around the following error, however.但是,我无法解决以下错误。
Cannot convert value of type '(Any) throws -> Bool? to expected argument type '(Any) throws -> Bool?
I'm trying to combine two arrays which contain multiple dictionaries.我正在尝试组合包含多个字典的两个数组。 I'm comparing the "name" in each dictionary in the array.我正在比较数组中每个字典中的“名称”。
let array2Name = array2.flatMap({$0["name"]}) // ["eli", "will"]
array2 = array1.reduce(array2) { !array2Name.contains(where: $1["name"] as! (Any) throws -> Bool? ?? "") ? $0 + [$1] : $0 }
print(array2)
You aren't properly comparing the value that is passed into the contains closure.您没有正确比较传递给 contains 闭包的值。 And you can't use the $
variable inside a closure within the closure.并且您不能在闭包内的闭包中使用$
变量。
let array2Name = array2.flatMap { $0["name"] }
array2 = array1.reduce(array2, { result, value in
!array2Name.contains(where: { $0 == value["name"] ?? "" }) ? result + [value]: result
})
print(array2)
Edit: This does away with the array2Name
value.编辑:这消除了array2Name
值。
array2 = array1.reduce(array2) { result, value in
result.contains(where: { $0["name"] == value["name"] }) ? result: result + [value]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.