如何解决 'scanLocation' 在 iOS 13.0 中已弃用

How to resolve 'scanLocation' was deprecated in iOS 13.0

当我尝试使用扫描仪时,我收到警告说 'scanLocation' 在 iOS 13.0 中已被弃用。由于能够从下一个位置开始扫描是扫描字符串的基础,想知道使用什么来代替 scanLocation。 Apple 的 Scanner 文档甚至没有提及弃用,更不用说建议用什么代替 scanLocation 了。

使用已弃用的 scanLocation 的示例:

while !scanner.isAtEnd {
    print(scanner.scanUpToCharacters(from: brackets))
    let block = scanner.string[scanner.currentIndex...]
    print(block)
    scanner.scanLocation = scanner.scanLocation + 1
}

tl;dr - 在 Swift.

中使用 Scanner 时使用 currentIndex 而不是 scanLocation

Apple 为糟糕的文档感到羞耻。但根据 Objective-C 版本扫描仪的 NSScanner.h 文件中的信息,仅在 Swift 中,scanLocation 属性 已被弃用并替换为 currentIndex 属性.

@rmaddy 已经给出了正确答案,但这显示了如何递增 currentIndex 因为它不同于仅将 1 添加到 scanLocation.

while !scanner.isAtEnd {
    print(scanner.scanUpToCharacters(from: brackets))
    let block = scanner.string[scanner.currentIndex...]
    print(block)
    scanner.currentIndex = scanner.string.index(after: scanner.currentIndex)
}