gpt4 book ai didi

ios - 无法从 plist 读取 CLLocationDegrees

转载 作者:搜寻专家 更新时间:2023-11-01 07:12:08 24 4
gpt4 key购买 nike

我在处理以下 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

最佳答案

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

关于ios - 无法从 plist 读取 CLLocationDegrees,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44273460/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com