【问题标题】:Cannot read CLLocationDegrees from plist无法从 plist 中读取 CLLocationDegrees
【发布时间】:2017-05-30 23:33:30
【问题描述】:

我在关注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 加载数据。谁能告诉我我做错了什么?


编辑:

现在我遇到了这些错误:

【问题讨论】:

    标签: ios swift mkmapview property-list


    【解决方案1】:

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

    【讨论】:

    • 谢谢@paulo,现在我又遇到了编译错误。我刚刚编辑了我的问题给你看。
    • @ArnonRodrigues 作为快速修复,尝试在您的 plist 中将 , 替换为 . 以查看它是否修复了它。您可能需要使用NumberFormatter 进行正确的数字解析...
    • 我的错!经纬度数据不正确。更正后,.plist 似乎没问题。现在我在我的视图控制器上遇到了这个问题。刚刚更新了我的问题。
    • 你忘了初始化你的coord属性——它是nil
    • 你的意思是:var coord: EmployeeDetails = nil! ?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 2011-09-06
    相关资源
    最近更新 更多