返回列表
ios #Scanner

如何解决scanLocation在iOS 13.0中被废弃的问题?

FD
Flame Dream
2024年5月14日 1 分钟阅读 0

当Swift新项目或者是老项目升级出现了Scanner 'scanLocation' was deprecated in iOS 13.0的问题,“scanLocation”在iOS 13.0中被废弃了。

scanLocation Warning的代码

convenience init(hexString: String) {
        let scanner = Scanner(string: hexString)
        scanner.scanLocation = 0   // 'scanLocation' was deprecated in iOS 13.0
        var rgbValue: UInt64 = 0
        scanner.scanHexInt64(&rgbValue)
}

解决办法:

您可以使用currentIndex而不是scanLocation

scanner.currentIndex = hexString.startIndex

最终的代码如下:

convenience init(hexString: String) {
        let scanner = Scanner(string: hexString)

        if #available(iOS 13.0, *) {
            scanner.currentIndex = hexString.startIndex
        } else {
            scanner.scanLocation = 0
        }

        var rgbValue: UInt64 = 0
        scanner.scanHexInt64(&rgbValue)
}
分享