[swift-users] Unexpected results when using String.CharacterView.Index
Zhao Xin
owenzx at gmail.com
Thu Mar 9 01:27:23 CST 2017
When using subscript of `String.CharacterView`, I got an unexpected error.
fatal error: Can't form a Character from an empty String
func test() {
let s = "Original Script:"
let cs = s.characters
// let startIndex = cs.startIndex
let nextIndex = "Original ?".characters.endIndex
let nextCharacter = cs[nextIndex]// above error
}
test()
However, if I chose another way to get the nextIndex. It works.
func test() {
let s = "Original Script:"
let cs = s.characters
let startIndex = cs.startIndex
// let nextIndex = "Original ?".characters.endIndex
let nextIndex01 = cs.index(startIndex, offsetBy: "Original ?".characters
.count)
let nextCharacter = cs[nextIndex01]
}
test()
Further more, I compared the two `nextIndex`. They were equal.
func test() {
let s = "Original Script:"
let cs = s.characters
let startIndex = cs.startIndex
let nextIndex = "Original ?".characters.endIndex
let nextIndex01 = cs.index(startIndex, offsetBy: "Original ?".characters
.count)
let nextCharacter = cs[nextIndex01]
print(nextIndex01 == nextIndex) // true
}
test()
So I wonder, is there a bug here?
Xcode 8.2.1 (8C1002), Swift 3.0.2
Zhaoxin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170309/c1f700b5/attachment.html>
More information about the swift-users
mailing list