From the documentation for the index(_:offsetBy:)
function:
The value passed as
distance
must not offseti
beyond the bounds of the collection.
Clearly this is what’s causing your error. You shouldn’t be assuming that this is a valid index for every fileName
you might have. You will need to code defensively around those lines to ensure you don’t crash. It’s almost never a good idea to rely on hardcoded indices like that.