Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 4: 'substring(to:)' is deprecated [duplicate]

Tags:

ios

swift

swift4

I'm having trouble converting my Swift 3 code to Swift 4. I've managed to translate everything else in the app successfully, but am having trouble with a single line of code:

cleanURL = cleanURL.substring(to: cleanURL.index(before: cleanURL.endIndex))

The error I'm getting is this:

ViewController.swift:62:33: 'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range upto' operator.
like image 782
Justin Bush Avatar asked Aug 13 '17 17:08

Justin Bush


1 Answers

Well, do what the error says,use String slicing subscript with a 'partial range upto' operator:

let actuallyCleanURL = kindaCleanURL[..<kindaCleanURL.endIndex]

Note that this returns a Substring. If you need to do more slicing operations, do them on this substring. Once you're done, promote your to a String by running it through the String initializer (String(mySubString)), causing a copy of the memory to be made.

like image 126
Alexander Avatar answered Oct 03 '22 15:10

Alexander