Why can't you subscript a Swift String with an Int?
WHAT IT TESTS: understanding Unicode-correct strings. OUTLINE: Characters are extended grapheme clusters of variable byte width, so integer offsets are not O(1) or meaningful; String.Index is an opaque position you advance via the collection.
WHAT IT TESTS: whether you grasp that Swift strings are sequences of Unicode grapheme clusters, not fixed-width bytes. ANSWER OUTLINE: because characters vary in underlying byte length, an Int subscript could not be O(1) and would tempt unsafe assumptions, so Swift forbids it; String.Index is an opaque marker into the storage that you obtain from startIndex and advance with index(after:) or index(_:offsetBy:). RED FLAG: treating a Swift String like a C char array, assuming one character equals one byte, or thinking the restriction is arbitrary.
Read the original → interview
- #ios
- #swift
- #string
- #unicode
- #performance
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.