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

Viewed 23946

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)
    }
7 Answers

In place of substring use suffix. Use like below :

cell.detailTextLabel?.text = String(thisRecord.pubDate.suffix(from: index))

It means you should use the new partial range operator as your upperBound:

let str =  "Hello World !!!"
if let index = str.range(of: "Hello ")?.upperBound {
   let string = String(str[index...])  // "World !!!"
}

In your case

cell.detailTextLabel?.text = String(thisRecord.pubDate[index...]))

In Swift 5, it is:

extension String {

    func index(from: Int) -> Index {
        return self.index(startIndex, offsetBy: from)
    }

    func substring(from: Int) -> String {
        let fromIndex = index(from: from)
        return String(self[fromIndex...])
    }

    func substring(to: Int) -> String {
        let toIndex = index(from: to)
        return String(self[..<toIndex])
    }

    func substring(with r: Range<Int>) -> String {
        let startIndex = index(from: r.lowerBound)
        let endIndex = index(from: r.upperBound)
        return String(self[startIndex..<endIndex])
    }
}

Most of my strings have A-Za-z and 0-9 content. No need for difficult Index handling. This extension of String is based on the familiar LEFT / MID and RIGHT functions.

extension String {

    // LEFT
    // Returns the specified number of chars from the left of the string
    // let str = "Hello"
    // print(str.left(3))         // Hel
    func left(_ to: Int) -> String {
        return "\(self[..<self.index(startIndex, offsetBy: to)])"
    }

    // RIGHT
    // Returns the specified number of chars from the right of the string
    // let str = "Hello"
    // print(str.left(3))         // llo
    func right(_ from: Int) -> String {
        return "\(self[self.index(startIndex, offsetBy: self.length-from)...])"
    }

    // MID
    // Returns the specified number of chars from the startpoint of the string
    // let str = "Hello"
    // print(str.left(2,amount: 2))         // ll
    func mid(_ from: Int, amount: Int) -> String {
        let x = "\(self[self.index(startIndex, offsetBy: from)...])"
        return x.left(amount)
    }
}

If you wish to get substring with specific offset without upper bound do the following:

let index = thisRecord.pubDate.index(thisRecord.pubDate.startIndex, offsetBy: 5)
cell.detailTextLabel?.text = String(thisRecord.pubDate[index...]

This way you create a new String object from your existing String thisRecord.pubDate taking anything from specified index to the end index of original String.

Related