Swift 4 ‘substring(from:)’ is deprecated: Please use String slicing subscript with a ‘partial range from’ operator

deprecatedsubstringswiftswift4

i've just converted my little app but i've found this error:
'substring(from:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator

my code is:

    let dateObj = dateFormatterFrom.date(from: dateStringa)


    if dateObj != nil {
        cell.detailTextLabel?.text = dateFormatterTo.string(from:(dateObj!))
    } else {
        let index = thisRecord.pubDate.index(thisRecord.pubDate.startIndex, offsetBy: 5)
        cell.detailTextLabel?.text = thisRecord.pubDate.substring(from: index)
    }

Best Answer

Follow the below example to fix this warning: Supporting examples for Swift 3, 4 and 5.

let testStr = “Test Teja”

let finalStr = testStr.substring(to: index) // Swift 3
let finalStr = String(testStr[..<index]) // Swift 4

let finalStr = testStr.substring(from: index) // Swift 3
let finalStr = String(testStr[index...]) // Swift 4 

//Swift 3
let finalStr = testStr.substring(from: index(startIndex, offsetBy: 3)) 

//Swift 4 and 5
let reqIndex = testStr.index(testStr.startIndex, offsetBy: 3)
let finalStr = String(testStr[..<reqIndex])

//**Swift 5.1.3 - usage of index**

let myStr = "Test Teja == iOS"

let startBound1 = String.Index(utf16Offset: 13, in: myStr)
let finalStr1 = String(myStr[startBound1...])// "iOS"

let startBound2 = String.Index(utf16Offset: 5, in: myStr)
let finalStr2 = String(myStr[startBound2..<myStr.endIndex]) //"Teja == iOS"