Fatal error: String index is out of bounds

From the documentation for the index(_:offsetBy:) function:

The value passed as distance must not offset i 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.

1 Like