无法转换类型' [String:String?]'的值预期的参数类型' [String:AnyObject?]'

时间:2015-10-30 13:54:47

标签: ios iphone swift mapkit

我是Swift的新手,我遵循了本教程:http://www.raywenderlich.com/90971/introduction-mapkit-swift-tutorial关于MapKit。问题是我在这行代码上出错了

func mapItem() -> MKMapItem {
    let addressDict = [String(kABPersonAddressStreetKey): self.subtitle]
    let placemark = MKPlacemark(coordinate: self.coordinate, addressDictionary: addressDict)

    let mapItem = MKMapItem(placemark: placemark)
    mapItem.name = self.title

    return mapItem
}

错误在标题上描述。包含此行的方法是:

{{1}}

请帮忙。

1 个答案:

答案 0 :(得分:14)

您需要将subtitle转换为AnyObject,如下所示:

let addressDict = [String(kABPersonAddressStreetKey): self.subtitle as! AnyObject]

您的完整代码将是:

func mapItem() -> MKMapItem {
    let addressDict = [String(kABPersonAddressStreetKey): self.subtitle as! AnyObject]
    let placemark = MKPlacemark(coordinate: self.coordinate, addressDictionary: addressDict)

    let mapItem = MKMapItem(placemark: placemark)
    mapItem.name = self.title

    return mapItem
  }