1

我刚刚将 ray wenderlich mapkit 教程升级为 MKPlacemark 的 xcode 7 错误。我仍然只是编码新手,我不确定从哪里开始解决这个错误。我搜索但知道有用。

谢谢你的帮助。非常感谢。

http://www.raywenderlich.com/90971/introduction-mapkit-swift-tutorial

这是我遇到错误的代码:

// annotation callout opens this mapItem in Maps app
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

错误是:

无法使用列表类型 'coordinate:CLLocationCoordinate2D, addressDictionary:[String : String?])' 的参数调用类型 'mkplacemark' 的初始化程序

再次感谢,

特拉维斯。

4

2 回答 2

2

您需要将您的字幕转换为 AnyObject,如下所示:

让 addressDict = [String(kABPersonAddressStreetKey): self.subtitle as!任何对象]

您的“func mapItem() -> MKMapItem { }”的完整代码将是:

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
  }
于 2016-07-04T11:11:14.437 回答
1

好的!

只是想通了我搜索得更深!

问题是 locationName 是可选的,因此 addressDictionary 被推断为与初始化程序不兼容的类型 [String:String?]。但是 [String:String] 类型的字典可以工作。

所以你可以替换这一行:

let addressDictionary = [String(CNPostalAddressStreetKey): subtitle]

有了这个:

let addressDictionary = [String(CNPostalAddressStreetKey): subtitle!]

或者这个(考虑到字幕的实现,这等效):

let addressDictionary = [String(CNPostalAddressStreetKey): locationName]

谢谢!!!

于 2015-09-18T22:54:57.240 回答