看看这个程序:

let s = "1"
print(s.startIndex)
print(s.index(before: s.endIndex))
print(s.index(before: s.endIndex) == s.startIndex)

它返回:
Index(_rawBits: 0)
Index(_rawBits: 256)
true

因此,字符串中的相同位置用rawbits 0和256表示。为什么?

最佳答案

索引的原始位是一个实现细节。正如您在示例中看到的,这两个值是相等的(对于==,它们返回true)。
对于当前的实现,设置了位8,这不是位置的一部分。That's a cached value for the offset to the next grapheme cluster, which is 1 byte away.它告诉你下一个字母表有一个字节(直到你计算出尾数,它才知道)。

10-06 07:58