扩展一个数组的的下标,
传入一个下标的数组,返回这些下标对应的元素的数组
extension Array{
subscript(input : [Int]) ->ArraySlice {
get{
var result =ArraySlice()
for i in input {
assert(i<self.count,"数组越界")
result.append(self[i])
}
return result
}
set{
for(index, i) in input.enumerated() {
assert(i< self.count,"数组越界")
self[i] = newValue[index]
}
}
}
}
letarray01 = ["a","b","c","d"]
leta01 =array01[0]
letarray02 =array01[[0,1,3]]//类型为ArraySlice ,值为["a", "b", "d"]