Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 4: 'init' is deprecated. CountableRange is now Range [duplicate]

Tags:

swift

I have this String category:

extension String {

    subscript (i: Int) -> String {
        return self[Range(i ..< i + 1)]
    }

    subscript (r: Range<Int>) -> String {
        let range = Range(uncheckedBounds: (lower: max(0, min(count, r.lowerBound)),
                                            upper: min(count, max(0, r.upperBound))))
        let start = index(startIndex, offsetBy: range.lowerBound)
        let end = index(start, offsetBy: range.upperBound - range.lowerBound)
        return String(self[start ..< end])
    }
}

and Xcode is giving me a warning at this line: return self[Range(i ..< i + 1)]

'init' is deprecated: CountableRange is now Range. No need to convert any more.

It's a shame that even though I'm quite well experienced with Swift, I have no idea how to fix this. Question is: how to get rid of this warning.

Thank you!

like image 527
Glenn Posadas Avatar asked Oct 05 '18 05:10

Glenn Posadas


1 Answers

You don't need the Range.init. In other words, change:

return self[Range(i ..< i + 1)]

to:

return self[i ..< i + 1]
like image 141
rmaddy Avatar answered Oct 16 '22 00:10

rmaddy