【问题标题】:How to resolve 'scanLocation' was deprecated in iOS 13.0iOS 13.0 中已弃用如何解决“scanLocation”
【发布时间】:2020-03-04 08:54:37
【问题描述】:

在尝试使用扫描仪时,我收到警告说“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
}

【问题讨论】:

    标签: swift foundation


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      @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)
      }
      

      【讨论】:

      • 请问如何将其重置回“0”?例如。扫描仪.scanLocation = 0
      • 你没有。您只需创建一个 Scanner 的新实例
      • 你不应该可以吗:scanner.currentIndex = scanner.string.startIndex
      猜你喜欢
      • 1970-01-01
      • 2019-11-29
      • 1970-01-01
      • 2020-01-12
      • 2020-09-07
      • 1970-01-01
      • 2020-07-26
      相关资源
      最近更新 更多