Не могу прочитать CLLocationDegrees из plist
У меня возникли проблемы с этим следующим struct
:
struct EmployeeDetails {
let functionary: String
let imageFace: String
let phone: String
let latitude: CLLocationDegrees
let longitude: CLLocationDegrees
init(dictionary: [String: Any]) {
self.functionary = (dictionary["Functionary"] as? String) ?? ""
self.imageFace = (dictionary["ImageFace"] as? String) ?? ""
self.phone = (dictionary["Phone"] as? String) ?? ""
self.latitude = (dictionary["Latitude"] as! CLLocationDegrees)
self.longitude = (dictionary["Longitude"] as! CLLocationDegrees)
У меня нет ошибок компиляции, но, когда я запускаю приложение, я получаю эту ошибку времени выполнения:
Важно сказать, что я загружаю данные из списка. Кто-нибудь может показать мне, что я делаю не так?
РЕДАКТИРОВАТЬ:
1 ответ
Решение
Ошибка довольно очевидна: вы приводите строковое значение кNSNumber
,
Попробуйте это вместо этого:
let latitudeStr = dictionary["Latitude"] as! String
self.latitude = CLLocationDegrees(latitudeStr)!
и вы должны сделать то же самое с "Longitude"
собственность также;)
Вы также можете столкнуться с проблемами локализованных номеров. Попробуй это:
let numberFormatter = NumberFormatter()
numberFormatter.decimalSeparator = ","
numberFormatter.thousandSeparator = "."
...
self.latitude = numberFormatter.number(from: latitudeStr)!.doubleValue