如何解决“scanLocation”在 iOS 13.0 中已被弃用

Chu*_*ger 18 foundation swift

尝试使用扫描仪时,我收到警告:iOS 13.0 中不推荐使用“scanLocation”。由于能够从下一个位置进行扫描是扫描字符串的基础,因此想知道使用什么来代替 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
}
Run Code Online (Sandbox Code Playgroud)

rma*_*ddy 26

tl;dr -在 Swift 中使用currentIndex而不是scanLocation使用Scanner

为糟糕的文档而对 Apple 感到羞耻。但是根据 NSScanner.h 文件中的信息,Objective-C 版本的 Scanner,只有在 Swift 中,该scanLocation属性已被弃用并替换为该currentIndex属性。


Chu*_*ger 10

@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)
}
Run Code Online (Sandbox Code Playgroud)

  • 请问如何将其重置回“0”?例如。扫描仪.扫描位置 = 0 (3认同)
  • 你不应该能够这样做:`scanner.currentIndex =scanner.string.startIndex`? (2认同)