Dictionary 参数类型';[MGLPointAnnotation]';不符合预期的类型';MGLAnnotation';

Dictionary 参数类型';[MGLPointAnnotation]';不符合预期的类型';MGLAnnotation';,dictionary,annotations,mapbox,unwrap,Dictionary,Annotations,Mapbox,Unwrap,我最近开始学习swift,现在已经迈出了制作自己地图的下一步。我已经成功地用mapkit制作了一个应用程序,现在我正在用mapbox试试运气,因为我喜欢它的设计特点 我已经构建了一个字典,它保存了我的位置数据,然后我做了一个循环,将数据添加到我的地图中。但是,我现在被卡住了,因为我遇到了以下错误: 参数类型“[MGLPointAnnotation]”与预期的类型“MGLAnnotation”不一致 当我尝试使用字典打开mapView.addAnnotation时会发生这种情况。我了解到这与打开字

我最近开始学习swift,现在已经迈出了制作自己地图的下一步。我已经成功地用mapkit制作了一个应用程序,现在我正在用mapbox试试运气,因为我喜欢它的设计特点

我已经构建了一个字典,它保存了我的位置数据,然后我做了一个循环,将数据添加到我的地图中。但是,我现在被卡住了,因为我遇到了以下错误:

参数类型“[MGLPointAnnotation]”与预期的类型“MGLAnnotation”不一致

当我尝试使用字典打开mapView.addAnnotation时会发生这种情况。我了解到这与打开字典中的值有关,但当我通过编写mapView.addAnnotation(注释为!MGLAnnotation)强制打开时,我的应用程序崩溃

有人能告诉我正确的方向吗?我不是在寻找修补程序,而是在找出哪里出了问题。非常感谢

代码如下:

let locations = [
        ["name" : "Apple Inc.",
         "latitude" : 37.33187,
         "longitude" : -122.02951,
         "mediaURL" : "http://www.apple.com"],
        ["name" : "BJ's Restaurant & Brewhouse",
         "latitude" : 37.33131,
         "longitude" : -122.03175,
         "mediaURL" : "http://www.bjsrestaurants.com"]
    ]

    var annotations = [MGLPointAnnotation]()

    for dictionary in locations {
        let latitude = CLLocationDegrees(dictionary["latitude"] as! Double)
        let longitude = CLLocationDegrees(dictionary["longitude"] as! Double)
        let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
        let name = dictionary["name"] as! String
        let discription = dictionary["mediaURL"] as! String
        let annotation = MGLPointAnnotation()
        annotation.coordinate = coordinate
        annotation.title = "\(name)"
        annotation.subtitle = "\(discription)"
        annotations.append(annotation)
    }

mapView.addAnnotation(annotations as! MGLAnnotation)

您正试图将某个对象的数组强制转换为另一个对象的单个对象…
annotations
是数组,而
MGLAnnotation
是单个对象

所以改变
mapView.addAnnotation(注释为!MGLAnnotation)
mapView.addAnnotation(注释为![MGLAnnotation])

这将删除您的错误…但是,您也在使用方法
addAnnotation
,该方法只需要1个对象作为参数。因此,请尝试:
mapView.addAnnotation(注解[0])

注意:我的第一个解决方案只是演示了错误的解决方案-它仍然无法工作,因为“只需要一个对象”的情况


希望这能帮助并解释一些事情:)

您正试图将某个数组强制转换为另一个的单个对象……
注释
是数组,而
MGLAnnotation
是单个对象

所以改变
mapView.addAnnotation(注释为!MGLAnnotation)
mapView.addAnnotation(注释为![MGLAnnotation])

这将删除您的错误…但是,您也在使用方法
addAnnotation
,该方法只需要1个对象作为参数。因此,请尝试:
mapView.addAnnotation(注解[0])

注意:我的第一个解决方案只是演示了错误的解决方案-它仍然无法工作,因为“只需要一个对象”的情况


希望这能帮助并解释一些事情:)

我仍然遇到这个问题。如果有人有解决方案,或者只是一个我可以找到更多信息的大致方向,我会非常感激!我仍然遇到这个问题。如果有人有解决方案,或者只是一个我可以找到更多信息的大致方向,我会非常感激!