我在处理以下 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)
我没有编译错误但是,当我运行应用程序时,我得到了这个运行时错误:
重要的是要说明我正在从 plist 加载数据。谁能告诉我我做错了什么?
编辑:
最佳答案
错误非常明显:您正在将字符串类型的值转换为 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
关于ios - 无法从 plist 读取 CLLocationDegrees,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44273460/