'Scanner.scanUpTo(_:into:)' was deprecated in iOS 13.0

What is the work around for Scanner.'scanUpTo(_:into:)' being deprecated in iOS 13.0?

Accepted Reply

Your code would become something like this with `scanUpToString(_:)`.

while
    let sid = scanner.scanUpToString("|"),
    let v1 = scanner.scanUpToString("|"),
    let v2 = scanner.scanUpToString("|"),
    let v3 = scanner.scanUpToString("|"),
    let v4 = scanner.scanUpToString("|"),
    let v5 = scanner.scanUpToString("\n")
{
    // Process v1 thru v5
}

To me, this code, with Conditional Binding, looks far more Swifty and more elegant and no more complicated.

You have no need to prepare v1 ... v5, with wider scope than needed, which would be preferrable in many cases.

Replies

It is frustrating that these depreactions don't include any helpful information on migrating. Everyone using the Scanner APIs is going to be going through this.


Instead of doing this:


        var str: NSString?
        if scanner.scanUpTo("***", into: &str) {
            // do stuff…
        }


You should now do this:


        if let str: String = scanner.scanUpToString("***") {
            // do stuff…
        }

What I am currently doing is this:


while (               
                scanner.scanUpTo("|", into: &sid) &&
                    scanner.scanUpTo("|", into: &v1) &&
                    scanner.scanUpTo("|", into: &v2) &&
                    scanner.scanUpTo("|", into: &v3) &&
                    scanner.scanUpTo("|", into: &v4) &&
                    scanner.scanUpTo("\n", into: &v5))
            {
                // Process v1 thru v5
            }


This is much more elegant and less complicated than the alternative, right? Is there an easier way to do this?

Your code would become something like this with `scanUpToString(_:)`.

while
    let sid = scanner.scanUpToString("|"),
    let v1 = scanner.scanUpToString("|"),
    let v2 = scanner.scanUpToString("|"),
    let v3 = scanner.scanUpToString("|"),
    let v4 = scanner.scanUpToString("|"),
    let v5 = scanner.scanUpToString("\n")
{
    // Process v1 thru v5
}

To me, this code, with Conditional Binding, looks far more Swifty and more elegant and no more complicated.

You have no need to prepare v1 ... v5, with wider scope than needed, which would be preferrable in many cases.

Thank you! That worked.

// how I worked around Scanner.'scanUpTo(_:into:)' being deprecated in iOS 13.0
public func extractLinesFrom(string: String) -> [String] {
  var result: [String] = []
  let scanner = Scanner(string: string)
  while scanner.isAtEnd == false {
    guard let line = scanner.scanUpToCharacters(from: .newlines) else { return result }
    result.append(line)
  }
  return result
}


The API drops the need for NSString local variable as well as makes use of optional return type so we can bail out early.