2016-10-08 29 views

回答

2

在Swift 3中,advancedBy()已更名爲advanced(by: Int)

此外,substringWithRange已更名爲substring(with: Range)

0

錯誤(有時在 「問題導航」,更容易看到按選項 + )說:

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

因此,你可以這樣做:

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

或者

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