I learned that Swift strings cannot be indexed by integer values. I remembered it and I use the rule. But I've never fully understood the mechanic behind it.
The explanation of from the official document is as follows
"Different characters can require different amounts of memory to store, so in order to determine which Character is at a particular position, you must iterate over each Unicode scalar from the start or end of that String. For this reason, Swift strings cannot be indexed by integer values"
I've read it several times, I still don't quite get the point. Can someone explain me a bit more why Swift String cannot be indexed by integer values?
Many Thanks
myString[8]corresponds to the 7th canonical symbol inmyString. Your hint is absolutely right, but the above is why I—and probably others too—look up the question.startIndex, regardless, e.g., the third character starts atlet start = string.index(string.startIndex, offsetBy: 2). I understand the rationale, but it doesn’t justify the added complexity.