ios - 使用 Swift 将 NSString 转换为 CLLocationCoordinate2DMake

标签 ios swift nsstring

如何将 NSString 转换为 CLLocationCoordinate2DMake。我想在 map 上做一个标记。这是我所做的,但出现“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

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

最佳答案

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

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

关于ios - 使用 Swift 将 NSString 转换为 CLLocationCoordinate2DMake,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28448714/

相关文章:

ios - 从 API 转换 Swift 中字符串中的任何 UTF-8 编码字符

ios - .usesFontLeading 不起作用

iOS:嵌套的推送动画会导致导航栏损坏

ios - 如何在不重新加载的情况下从 super View 中删除 View ?

ios - 检测 SearchController 何时出现在 UINavigationItem 中

ios - 你如何在 Swift 中使用 MBProgressHUD CustomView/Determinate Loader?

objective-c - 如果需要,正确使用格式说明符最多显示三位小数,否则显示零位小数?

ios - performSegueWithIdentifier 和 viewDidLoad

iphone - 我应该使用哪个 SDK 版本的 iPhone 应用程序? 4.2 还是 3.?

ios - 我在理解 "scaling transform coordinates"时遇到问题。需要帮助