gpt4 book ai didi

ios - 扩展 where 子句中的 ArrayType

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:57:45 27 4
gpt4 key购买 nike

enter image description here

extension Array where Element: _ArrayType, Element.Generator.Element: Any {
func transpose() -> [Element] {
if self.isEmpty { return [Element]() }
let count = self[0].count
var out = [Element](repeating: Element(), count: count)
for outer in self {
for (index, inner) in outer.enumerated() {
out[index].append(inner)
}
}
return out
}
}

从 Swift 2.2 转换后,我在 Swift 3.0 中遇到此错误。数组的元素也是数组。那么在Swift 3.0中如何定义呢?

最佳答案

extension Array where Element : Collection, Element.Index == Int, Element.IndexDistance == Int {
func transpose() -> [[Element.Iterator.Element]] {
typealias InnerElement = Element.Iterator.Element
if self.isEmpty { return [] }
let count = self[0].count
var out = [[InnerElement]](repeating: [InnerElement](), count: count)
for outer in self {
for (index, inner) in outer.enumerated() {
out[index].append(inner)
}
}
return out
}
}

关于ios - 扩展 where 子句中的 ArrayType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39366534/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com