【问题标题】:I am not able to add items to the "places" dictionary?我无法将项目添加到“地点”字典?
【发布时间】:2016-12-28 12:49:41
【问题描述】:

每当我将项目添加到“地点”字典时,它都会显示一个错误,我基本上希望我的应用程序在用户点击地图时添加注释,并希望注释具有“thoroughfare”和“subThoroughfare” . 然后注释文本应该更新到另一个 viewController.swift 文件中的表。我想用这个地方更新字典

它要求我用逗号替换分号。

这是全局变量的代码:

var places = [Dictionary<String,String>()] 

现在我在另一个 viewController.swift 文件中使用了这个变量我把这段代码放在 CLGeocode 函数下,当我附加字典时,它要求我用逗号替换分号。:

places.append("name":title, "lat":"\(newCoordinate.latitude)", "lon",:"\(newCoordinate.longitude)")

                let annotation = MKPointAnnotation()

                annotation.coordinate = newCoordinate

                annotation.title = title

                self.map.addAnnotation(annotation)

【问题讨论】:

    标签: swift2 ios9 xcode7.3


    【解决方案1】:
    places.append(["name":title, "lat":"\(newCoordinate.latitude)", "lon",:"\(newCoordinate.longitude)"])
    

    使用这个^

    【讨论】:

    • 预期的分隔符','
    • 在名称之前添加左括号和右括号,然后是最后一个字段值
    【解决方案2】:

    以更安全的方式尝试您的代码:-

     var places = [[String: Any]]()
    
     places.append(["name": title ?? "", "lat": newCoordinate.latitude ?? 0.0, "lon": newCoordinate.longitude ?? 0.0])
    

    【讨论】:

    • 谢谢阿南德,这对我很有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    • 2020-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多