Colin Hong
Colin Hong

Reputation: 337

'advancedBy' in Swift is unavailable

error messages

I'm not sure how to resolve the above error messages, I have tried to convert it to index but it seems like it does not accept strings.

I'm not sure how advancedBy works too as well. Help is very much appreciated!

Upvotes: 3

Views: 2478

Answers (2)

Rob
Rob

Reputation: 437622

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]

Upvotes: 0

perhapsmaybeharry
perhapsmaybeharry

Reputation: 894

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

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

Upvotes: 2

Related Questions