swift 泛型数组的扩展
swift extension for generics array
我想return [S:M]
类型,但是Xcode会提示Single argument function types require parentheses
,有什么办法可以解决这个问题,请帮助我,谢谢
extension Array {
func merge<S, M>() where Element == [S:M] -> [S:M] {
var res: [S:M] = [:]
forEach { (element) in
element.forEach({ (obj) in
res[obj.key] = obj.value
})
}
return res
}
}
-> [S:M]
需要放在where
子句之前。
extension Array {
func merge <S, M> () -> [S: M] where Element == [S: M] {
var res: [S: M] = [:]
self.forEach { element in
element.forEach { obj in
res[obj.key] = obj.value
}
}
return res
}
}
我想return [S:M]
类型,但是Xcode会提示Single argument function types require parentheses
,有什么办法可以解决这个问题,请帮助我,谢谢
extension Array {
func merge<S, M>() where Element == [S:M] -> [S:M] {
var res: [S:M] = [:]
forEach { (element) in
element.forEach({ (obj) in
res[obj.key] = obj.value
})
}
return res
}
}
-> [S:M]
需要放在where
子句之前。
extension Array {
func merge <S, M> () -> [S: M] where Element == [S: M] {
var res: [S: M] = [:]
self.forEach { element in
element.forEach { obj in
res[obj.key] = obj.value
}
}
return res
}
}