简体   繁体   English

Swift:无法转换类型“(Any) throws -> Bool”的值? 到预期的参数类型 '(Any) throws -> Bool?

[英]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.

相关问题 无法将'String'类型的值转换为预期的参数类型'(Any)引发-> Bool' - Cannot convert value of type 'String' to expected argument type '(Any) throws -> Bool' Swift:无法将'String'类型的值转换为预期的参数类型'@noescape(AnyObject)引发-> Bool' - Swift: Cannot convert value of type 'String' to expected argument type '@noescape (AnyObject) throws -> Bool' 无法将“Type”类型的值转换为预期的参数类型“(Type) throws -> Bool” - Cannot convert value of type 'Type' to expected argument type '(Type) throws -> Bool' 无法将'Option'类型的值转换为预期的参数类型'@noescape(Option)throws - > Bool' - Cannot convert value of type 'Option' to expected argument type '@noescape (Option) throws -> Bool' 无法将“[dataModel]”类型的值转换为预期的参数类型“(dataModel) throws -> Bool” - Cannot convert value of type '[dataModel]' to expected argument type '(dataModel) throws -> Bool' 无法转换“ Meme!”类型的值 到预期的参数类型'@noescape(Meme)throws-> Bool' - Cannot convert value of type 'Meme!' to expected argument type '@noescape (Meme) throws -> Bool' 无法将类型'Int'的值转换为预期的参数类型'@noescape(Int?)throws-> Bool' - Cannot convert value of type 'Int' to expected argument type '@noescape (Int?) throws -> Bool' 无法将“String”类型的值转换为预期的参数类型“[Any]” - Cannot convert value of type 'String' to expected argument type '[Any]' IOS/Swift:如何在the.firstIndex中表示$0的第一个position? '无法将类型 'Any' 的值转换为预期的参数类型 'String'' - IOS/Swift: How to indicate the first position of the $0 in the .firstIndex ? 'Cannot convert value of type 'Any' to expected argument type 'String'' swift:无法将类型“()”的值转换为预期的参数类型“ [Double]” - swift : Cannot convert value of type '()' to expected argument type '[Double]'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM