无法从 plist 读取 CLLocationDegrees
Cannot read CLLocationDegrees from plist
我遇到了一些问题 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)
我没有编译错误但是,当我运行应用程序时,我得到这个运行时间错误:
重要的是要说我正在从 plist 加载数据。谁能告诉我我做错了什么?
编辑:
现在我遇到了这些错误:
错误很明显:您正在将字符串类型的值转换为 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
我遇到了一些问题 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)
我没有编译错误但是,当我运行应用程序时,我得到这个运行时间错误:
重要的是要说我正在从 plist 加载数据。谁能告诉我我做错了什么?
编辑:
现在我遇到了这些错误:
错误很明显:您正在将字符串类型的值转换为 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