I’m studying the Collection protocol and decided to implement my own collection, but I didn’t stop there and wanted to implement the index(after:) method so that it works the other way around
struct SimpleCollection: Collection {
var numbers = [1, 2, 3, 4, 5]
var startIndex: Int { numbers.startIndex }
var endIndex: Int { numbers.endIndex }
subscript(index: Int) -> Int {
return numbers[index]
}
func index(after i: Int) -> Int {
return numbers.index(after: i)
}
}
Here is my structure and I want to implement the Index(after:) method so that it is output in the opposite direction
let test = SimpleCollection();
for items in test{
print(items)
}
What is being displayed now
1
2
3
4
5
What i want
5
4
3
2
1