iosswiftmkmapviewproperty-list

Cannot read CLLocationDegrees from plist


I am having some trouble with this following 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)

I have no compiling errors but, when I run the app, I get this runtime error:

error

It's important to say that I am loading data from a plist. Anyone could show me what am I doing wrong?


EDIT:

Now I am having these errors: enter image description here


Solution

  • The error is pretty clear: you are casting a string-typed value to a NSNumber.

    Try this instead:

    let latitudeStr = dictionary["Latitude"] as! String
    self.latitude = CLLocationDegrees(latitudeStr)!
    

    and you should do the same thing to the "Longitude" property as well ;)

    You also may be running into localized numbers issues. Try this:

    let numberFormatter = NumberFormatter()
    numberFormatter.decimalSeparator = ","
    numberFormatter.thousandSeparator = "."
    ...
    self.latitude = numberFormatter.number(from: latitudeStr)!.doubleValue