'subscript' is unavailable: cannot subscript String with a CountableClosedRange, see the documentation comment for discussion

前端 未结 8 960
谎友^
谎友^ 2021-01-31 13:57

In Swift 4, I\'m getting this error when I try to take a Substring of a String using subscript syntax.

\'subscript\' is unavaila

8条回答
  •  轮回少年
    2021-01-31 14:55

    Your question (and self-answer) has 2 problems:

    Subscripting a string with Int has never been available in Swift's Standard Library. This code has been invalid for as long as Swift exists:

    let mySubstring: Substring = myString[1..<3]
    

    The new String.Index(encodedOffset: ) returns an index in UTF-16 (16-bit) encoding. Swift's string uses Extended Grapheme Cluster, which can take between 8 and 64 bits to store a character. Emojis make for very good demonstration:

    let myString = "

提交回复
热议问题