1

Swift is said to be designed with safety in mind. If that’s so, then why doesn’t it have a built-in solution for the “index out of range” error?

It could, for example, return optional values when array subscripts are used:

let friends = ["Jack", "Lisa", "Brian"]

let friend1 = friends[1] // Optional("Lisa")
let friend3 = friends[3] // nil

Dictionaries do use that pattern, and there’s no problem with that.

I know I can compare array indices with friends.count, but doing so constantly is tiresome.

I just don’t understand why this thing isn’t solved by Swift designers already. Maybe there are some widespread conventions or technical restrictions I’m not aware of. In that case, I’d be grateful for an explanation.

Yakov Manshin
  • 664
  • 9
  • 24
  • 4
    This is safety. This is *much* better than blindly accessing values outside of the array. – rmaddy Jan 21 '19 at 17:47
  • 4
    Also keep in mind that it is a programming error when you write code that tries to access a bad index. It should crash. You should fix it. – rmaddy Jan 21 '19 at 17:48
  • See: https://stackoverflow.com/q/25329186/1630618 – vacawama Jan 21 '19 at 17:49
  • 5
    One last point - you have an array of non-optional values. Your suggestion is to have the index access return an optional when provided a bad index. So you end up writing code to deal with optionals in place of writing code to ensure you have a valid index. No benefit there. – rmaddy Jan 21 '19 at 17:51
  • 1
    @rmaddy I understand that such situations should never happen, but they still do, in rare occasions. The most basic optional chaining could be used for unwrapping Optionals, and even displaying an empty string (just by adding `?? ""` to the end of the statement) is so much better than letting the app crash. – Yakov Manshin Jan 21 '19 at 17:56
  • 1
    @rmaddy Besides, with dictionaries, there’s no problem with the fact that values returned in result of subscripting are optional. I can’t see why it should be a problem with arrays. – Yakov Manshin Jan 21 '19 at 17:59
  • @YakovM. why would you need to use a random index to access the collection? And if you need so, you just need to generate the random index within the indices range. – Leo Dabus Jan 21 '19 at 18:13
  • Btw if you need a safe subscript implementation that doesn't access the count property you cause this one https://stackoverflow.com/a/38215613/2303865 `extension BidirectionalCollection { subscript(safe offset: Int) -> Element? { guard !isEmpty, let i = index(startIndex, offsetBy: offset, limitedBy: index(before: endIndex)) else { return nil } return self[i] } }` – Leo Dabus Jan 21 '19 at 18:15
  • 2
    There is a (long) discussion in the Swift forum: https://forums.swift.org/t/add-accessor-with-bounds-check-to-array/16871 – Martin R Jan 21 '19 at 18:30
  • There is a slight difference here between dictionaries and array: the set of indices of a dictionary is most of the times infinite, while an array has a clearly defined set of indices: `0.. – Cristik Jan 22 '19 at 06:10

0 Answers0