iOS Dev Nugget 307 Array Out of Bounds


Need to run a code review on your codebase? Hire me

Swift has strong type checking. Generally, I've found that if you write your Swift apps without using force unwraps, and not deliberately stepping out of Swift's type checking, you end up with an app that hardly crashes. There is 1 exception though. Array random access via index is unchecked:

let a = [1, 2, 3]

This will crash with:

Fatal error: Index out of range

In this case, Swift code that compiles may still crash. So, either check before accessing (explicitly, or by implementing and using your own checked subscript operator) unless you are sure.

Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No



Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter


View archives of past issues