Safe Bounds Check for Collection

Swiftos
2 min readJan 27, 2023

--

Photo by Ümit Yıldırım on Unsplash

Safety is always essential, especially when you are working in a team.

What will happen if you run the following code snippet?

You will get an “Index out of bounds” run-time error, and the application will crash. Companies never want to risk their apps crashing. Even though you are 99.99% sure that it won’t crash, checking the index before accessing the elements of a Collection is highly recommended, especially if you are not the only one working on the codebase.

You can add a handy Collection extension to your project for safe bounds check.

To use it.

Now, the application won’t crash, and “Index out of bounds” will be printed out ;)

Become a Medium member, read more great stories and grow your career.

--

--

Swiftos

I publish short iOS development tutorials on a weekly basis. For all content, head over to: https://www.patreon.com/Swiftos