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

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"

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)