Swift-NumberFormatter字符串中的数字对iPhone 8返回零,但对其他设备有效



我正在开发一款iOS应用程序,该应用程序使用地图上的位置进行绘制。下面的代码用于创建一个CLLocationCoordinate2D对象。

let locationData = ["12.9716","77.5946"]
let latitude = NumberFormatter().number(from: locationData[0] as! String)
let longitude = NumberFormatter().number(from: locationData[1] as! String)
print("Latitude Info -> (String(describing: latitude?.doubleValue))")
print("Longitude Info -> (String(describing: longitude?.doubleValue))")
let location = CLLocationCoordinate2DMake((latitude?.doubleValue)!, (longitude?.doubleValue)!)

该代码适用于iPhone 6和其他设备,但在iPhone 8上,纬度和经度值返回零。

这种行为很奇怪,因为这种情况是特定于特定设备集的。

问题通过使用Double(locationData[0] as! String)解决,但希望了解这种行为的原因。

尝试检查iPhone的系统语言和区域设置,这些设置返回nil。

NumberFormatter具有"locale"属性,默认值为locale.current.

例如。当区域设置为"en-DE"时,"12.9716"将返回nil。

相关内容

最新更新