使用Swift将NSString转换为CLLocationCoordinate2DMake

时间:2015-02-11 07:25:45

标签: ios swift nsstring

如何将NSString转换为CLLocationCoordinate2DMake。我想在地图上做一个标记。这是我所做的,但我得到“NSString不能转换为CLLocationDegrees”错误。

for openComplain in openComplains {

                                var lat = openComplain["lat"] as NSString
                                var long = openComplain["lng"] as NSString
                                NSLog("Lat is %@",lat)
                                NSLog("long is %@",long)




                                var complainLoc = CLLocationCoordinate2DMake(lat, long)

                                var marker : GMSMarker = GMSMarker(position: complainLoc)
                                marker.title = "Complain 1"
                                marker.map = self.mapView
                            }

我试过了

var lat = (openComplain["lat"] as NSString).doubleValue
var long = (openComplain["lng"] as NSString).doubleValue

但是在那种情况下我得到了空值。请帮忙。

1 个答案:

答案 0 :(得分:3)

在将字典转换为Double之前,您只需解开字典可选值:

var lat  = (openComplain["lat"]! as NSString).doubleValue
var long = (openComplain["lng"]! as NSString).doubleValue