'advancedBy' in Swift is unavailable

前端 未结 2 1256
盖世英雄少女心
盖世英雄少女心 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:43

    In Swift 3, advancedBy() has been renamed to advanced(by: Int).

    Additionally, substringWithRange has been renamed to substring(with: Range).

    0 讨论(0)
  • 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]
    
    0 讨论(0)
提交回复
热议问题