在 Swift 中,String.CharacterView 的实例计数是否在所有情况下都始终等于 startIndex 和 endIndex 之间的距离?

In Swift, is the count of an instance of String.CharacterView always equal to the distance between the startIndex and endIndex in all cases?

标题说明了一切。在 Swift 中,String.CharacterView 的实例计数是否在所有情况下都始终等于 startIndex 和 endIndex 之间的距离?

Swift 2.3:

string.characters.count == string.characters.startIndex.distanceTo(string.characters.endIndex)

Swift 3:

string.characters.count == string.characters.distance(来自:string.characters.startIndex, to:string.characters.endIndex)

是的。这是Collection的要求。从 startIndexendIndex 的迭代必须完全覆盖所有元素。所采取的步数(距离)必须等于 count 否则您将违反该要求。