iOS 13.0中不建议使用“ scanLocation”解决方法

时间:2019-11-07 17:50:08

标签: swift foundation

当尝试使用扫描仪时,我收到警告,称“ scanLocation”在iOS 13.0中已被弃用。由于能够从下一个位置进行扫描对于扫描String至关重要,因此想知道使用什么代替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
}

2 个答案:

答案 0 :(得分:4)

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

对于可怜的文档,Apple感到羞耻。但是基于NSScanner.h文件中有关Scanner的Objective-C版本的信息,仅在Swift中,scanLocation属性已被弃用,并由currentIndex属性替换。

答案 1 :(得分:0)

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

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