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
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Reply via email to