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

前端 未结 8 1015
谎友^
谎友^ 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:36

    You getting this error because result of subscript with range is Substring? not Substring.

    You must use following code:

    let myString: String = "foobar"
    let mySubstring: Substring? = myString[1..<3]
    

提交回复
热议问题