//: FROM https://www.anuomob.com
import UIKit
protocol Container{
associatedtype Item
mutating func append(_ item:Item)
var count:Int{get}
subscript(i:Int)->Item {get}
}
extension Container{
subscript<Indices:Sequence>(indices:Indices)->[Item] where Indices.Iterator.Element == Int{
var result=[Item]()
for index in indices{
result.append(self[index])
}
return result
}
}
class IntStack: Container{
var items = [Int]()
subscript(i: Int) -> Int {
return items[i]
}
func append(_ item: Int) {
items.append(item)
}
var count: Int {
return items.count
}
typealias Item = Int
}
var intStack = IntStack()
intStack.append(1)
intStack.append(2)
print(intStack[[0,1]])
结果
[1, 2]