无法从plist中读取CLLocationDegrees

时间:2017-05-30 23:33:30

标签: 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

1 个答案:

答案 0 :(得分:2)

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