我试图找出count
数组中的名字和姓氏值出口,a
并[String: Int]
使用相同的键返回结果作为计数。
我在这条线上出错了newResult[arg.key] = counts
。无法将类型“ Int”的值分配给类型“ Int”?
func abbreviation(a:[String], b: [String : String]) ->[String : Int] {
let dict = b.reduce([String : Int]()){ (result, arg) in
var newResult = result
let counts = a.reduce(0) { (newcount, value) -> Int in
let count = newcount + (value.components(separatedBy:arg.value).count - 1)
return count
}
return newResult[arg.key] = counts
}
return dict
}
//结果
let dict = abbreviation(a:["This is chandan kumar and chandan kumar and new chandan","check chandan kumar","non ame"], b:["first":"chandan","last":"kumar"])
该错误消息是如此混乱,您可能需要习惯将其视为Swift cannot infer some types in this context
。
用这行:
return newResult[arg.key] = counts
您知道此return
陈述返回什么吗?这是一个Void
,也称为空元组。(这Void
是Swift中赋值语句的结果类型。)您可能期望newResult
闭包的结果,但是除非明确编写,否则这种事情不会发生return newResult
。
尝试将行更改为以下内容:
newResult[arg.key] = counts
return newResult
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句