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

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

    1. How can I resolve this error?

    This error means you can't use an Int in the subscript format – you have to use a String.Index, which you can initialize with an encodedOffset Int.

    let myString: String = "foobar"
    let lowerBound = String.Index.init(encodedOffset: 1)
    let upperBound = String.Index.init(encodedOffset: 3)
    let mySubstring: Substring = myString[lowerBound..
    1. Where is "the documentation comment for discussion" that was referred to in the error?

    It's on GitHub in the Swift Standard Library repository in a file called UnavailableStringAPIs.swift.gyb in the bottom of a locked filing cabinet stuck in a disused lavatory with a sign on the door saying 'Beware of the Leopard'. link

提交回复
热议问题