String slicing substring — Mastering Swift 4 — Fourth Edition

Code deprecation error in playground when I was trying the example mentioned in the page 79 of the book “Mastering Swift 4 — Fourth Edition”.

The code snippet:

path.substring(to: startIndex)
// returns the “/one”
path.substring(from: endIndex)
// returns the “/four”

The following are deprecated in Swift 4,

  1. substring(to:)
  2. substring(from:)

you get error in playground to change the code and use String slicing subscript with a 'partial range from' operator.

I found the solution for this question on stackoverflow https://stackoverflow.com/questions/45562662/how-can-i-use-string-slicing-subscripts-in-swift-4 solving the code deprecation error.

I used them as follows,

path[…startIndex]
// returns the “/one”
path[endIndex…]
// returns the “/four”

Junior Software Engineer | M.Sc. Computer Science from Technische Universitaet Berlin

Junior Software Engineer | M.Sc. Computer Science from Technische Universitaet Berlin

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store