Raghav
Raghav

Reputation: 645

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.

Upvotes: 0

Views: 518

Answers (1)

shaj04
shaj04

Reputation: 23

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.

Upvotes: 2

Related Questions