简体   繁体   中英

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 :

错误

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: 在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM