Coder Coder - 6 months ago 44
iOS Question

'advancedBy' API in Swift 2.0

'advance' API has been removed in Swift 2.0.

If we use 'advance' API in Swift 2.0, it is throwing error saying

'advance' is unavailable: call the '**advancedBy(n)**' method on the index
.

How to get index using advancedBy API in Swift 2.0?

Below is my scenario:

let startIndex = advance(cardNumber.endIndex, -4)
let lastFourDigitsOfPhoneNumber = phoneNumber.substringFromIndex(startIndex)

Answer

You need to call advancedBy on endIndex.

let startIndex = cardNumber.endIndex.advancedBy(-4)
let lastFourDigitsOfPhoneNumber = phoneNumber.substringFromIndex(startIndex)