繁体   English   中英

如何将JSON对象转换为CLLOCATIONDEGREES?

[英]How do I convert JSON object to CLLOCATIONDEGREES?

如何将JSON对象转换为CLLocationDegrees?

 if let value = response.result.value {

      let json = JSON(value)

      for (key,subJson):(String, JSON) in json {

            let lat = subJson["latitude"] as! CLLocationDegrees
            let lng = subJson["longitude"] as! CLLocationDegrees // This will return error



       }

  }

JSON的数据类型是

{
  "latitude": 2323.44555,
  "longitude": 2313.344555
}

只是行不通

错误讯息

从JSON转换为不相关的对象CLLocationDegrees(aka Double)总是失败

很清楚, SwiftyJSON库中的SwiftyJSONJSON返回JSON 您必须获取doubleValueCLLocationDegreesDouble的类型别名)

for (key,subJson) in json { // don't annotate types unless the compiler tells you
        let lat = subJson["latitude"].doubleValue
        let lng = subJson["longitude"].doubleValue
        // do something with lat and lng
}

这是你可以得到的

let json = [
    "latitude": 2323.44555,
    "longitude": 2313.344555
]
var location = CLLocationCoordinate2D()
if let lat = json["latitude"] as? Double,let lng = json["longitude"] as? Double {
        location.latitude = lat
        location.longitude = lng
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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