gpt4 book ai didi

arrays - 在 Swift 中,如何将数组中的数字字符串转换为 Doubles

转载 作者:可可西里 更新时间:2023-11-01 00:39:47 24 4
gpt4 key购买 nike

我有一个包含国家和坐标的大型 JSON 数组。示例元素如下所示:

["CountryName":"El Salvador","CapitalName":"SanSalvador","CapitalLatitude":"13.7","CapitalLongitude":"-89.200000","CountryCode":"SV","ContinentName":"Central America"],["CountryName":"Equatorial Guinea","CapitalName":"Malabo","CapitalLatitude":"3.75","CapitalLongitude":"8.783333","CountryCode":"GQ","ContinentName":"Africa"]

然后我有一个 for 循环,它应该根据数组中的数据创建注释。

for location in locations {
let annotation = MGLPointAnnotation()
annotation.title = location["CountryName"] as? String
annotation.coordinate = CLLocationCoordinate2D(latitude: location["CapitalLatitude"] as! Double, longitude: location["CapitalLongitude"] as! Double)
mapView.addAnnotation(annotation)
}

但除非我可以去掉纬度和经度的引号,否则这将不起作用。我试过这样做:

location["CapitalLatitude"] = Double(location[CapitalLatitude])
location["CapitalLongitude"] = Double(location[CapitalLongitude])

但是由于尝试连接不相关的类型值,这给了我一个错误。

错误是在展开一个可选值之后,一个值为 nil。我知道我没有正确提供纬度/经度数字。

如何去除纬度/经度数字周围的引号?有太多无法手动完成。

最佳答案

您正在尝试使用以下类型访问 location:

location["CapitalLatitude"] = Double(location["CapitalLatitude"])
location["CapitalLongitude"] = Double(location["CapitalLongitude"])

试试吧。

关于arrays - 在 Swift 中,如何将数组中的数字字符串转换为 Doubles,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45685808/

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