gpt4 book ai didi

swift - 为什么 startIndex 不等于 endIndex 在 Swift 中转移到 String 中的 startIndex 位置?

转载 作者:搜寻专家 更新时间:2023-11-01 05:54:37 24 4
gpt4 key购买 nike

看程序:

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

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

最佳答案

索引的原始位是一个实现细节。正如您在示例中看到的,这两个值相等(对于 ==,它们返回 true)。

至于当前的实现,第 8 位已设置,这不是位置的一部分。 That's a cached value for the offset to the next grapheme cluster, which is 1 byte away.它告诉您下一个字素有一个字节(在您计算 endIndex 之前它不知道)。

关于swift - 为什么 startIndex 不等于 endIndex 在 Swift 中转移到 String 中的 startIndex 位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56066091/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com