[英]Cannot read CLLocationDegrees from plist
I am having some trouble with this following struct
: 我在使用以下
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 . 重要的是要说我正在从plist加载数据 。 Anyone could show me what am I doing wrong?
有人可以告诉我我在做什么错吗?
EDIT: 编辑:
The error is pretty clear: you are casting a string-typed value to a NSNumber
. 错误非常明显: 您正在将字符串类型的值强制转换为
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 ;) 并且您也应该对
"Longitude"
属性执行相同的操作;)
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.