I forgot how to convert a reversed-collection index to its original index

I forgot, if you have the index of an element in a reversed collection, how do you get the element's index in its original collection? I haven't messed with those for years; and I always got semi-confused which way it goes.

(For the newbies, reverse collections still use their original's startIndex and endIndex, but in an off-by-one banner to prevent accessing illegal indexes.)

1 Like

To find the position that corresponds with this index in the original, underlying collection, use that collection’s index(before:) method with the base property.

(This is why I love Swift Forums, sometimes you learn stuff from the question alone :wink: )

2 Likes