17

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.
Justin Bush
  • 4,548
  • 5
  • 33
  • 55
  • are you sure it makes sense to be mutating a variable, rather than defining a new one? If the url is "clean" after taking off the last symbol, why is it called "cleanURL" even before then? – Alexander Aug 13 '17 at 17:17
  • 1
    Check [this thread](https://stackoverflow.com/q/45562662/6541007). Seems to be a duplicate. – OOPer Aug 13 '17 at 17:18

1 Answers1

30

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.

Alexander
  • 54,014
  • 10
  • 87
  • 136