ios - 无法从 plist 读取 CLLocationDegrees

标签 ios swift mkmapview property-list

我在处理以下 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)

我没有编译错误但是,当我运行应用程序时,我得到了这个运行时错误:

error

重要的是要说明我正在从 plist 加载数据。谁能告诉我我做错了什么?


编辑:

现在我遇到了这些错误: enter image description here

最佳答案

错误非常明显:您正在将字符串类型的值转换为 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/

相关文章:

html - 为什么我的 Web 图片库中的元素在 iOS 上消失了?

iphone - 当我在 iOS7 中使用物理键盘在 UITextfield 上键入时出现错误

ios - 如何访问 UITableView 中具有多个部分的字典数组

ios - 如何知道发送的数据是什么类型?

ios - Swift Barcode Generation,使条码只扫描一次

ios - NSRangeException 崩溃无法删除 "heading"的观察者

ios - MKMapView 根据 bool 值自定义引脚

ios - 如何在 UITableViewCell 中调用 viewDidLoad?

ios - UINavigationBar 外观 addSubview 在 iOS 8 中不起作用

ios - 如何使用 Swift 显示自定义注释,即使它位于用户当前位置