简体   繁体   中英

Swift - NumberFormatter Number from string returns nil for iPhone 8 but works for other device

I am developing an iOS app which uses the location to draw on the map. Below code is used to create a CLLocationCoordinate2D object.

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)!)

The code works fine for the iPhone 6 and other devices but on iPhone 8 the latitude and longitude values returns nil.

This behaviour is weird as this is happens specific to certain device set.

The issue resolved by using Double(locationData[0] as! String) instead, but would like to know the reason for this behaviour.

Try to check the system language and region settings of the iPhone which return nil.

NumberFormatter has 'locale' property, which default value is locale.current.

eg. when locale is "en-DE", '12.9716' will return nil.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM