'advancedBy' in Swift is unavailable

前端 未结 2 1257
盖世英雄少女心
盖世英雄少女心 2021-01-20 19:56

\"error

I\'m not sure how to resolve the above error messages, I have tried to convert it to index b

2条回答
  •  南笙
    南笙 (楼主)
    2021-01-20 20:46

    The error (sometimes more easily seen in the "Issues Navigator", by pressing option+4) says:

    error: 'advancedBy' is unavailable: To advance an index by n steps call 'index(_:offsetBy:)' on the CharacterView instance that produced the index.

    Thus you could do:

    let r = testStr.index(testStr.startIndex, offsetBy: range.location) ..< testStr.index(testStr.startIndex, offsetBy: range.location + range.length)
    let result = testStr[r]
    

    Or

    let start = testStr.index(testStr.startIndex, offsetBy: range.location)
    let end = testStr.index(start, offsetBy: range.length)
    let result = testStr[start ..< end]
    

提交回复
热议问题