Swift 5.x 泛型下标

2020-07-11  本文已影响0人  ShenYj

e.g.

protocol Container {
    associatedtype ItemType
    mutating func append(_ item: ItemType)
    var count: Int { get }
    subscript(i: Int) -> ItemType { get }
}

extension Container {
    subscript<Indices: Sequence>(indices: Indices) -> [ItemType]
        where Indices.Iterator.Element == Int {
            var result = [ItemType]()
            
            for index in indices {
                result.append(self[index])
            }
            return result
    }
}

上一篇 下一篇

猜你喜欢

热点阅读