升级的光线wenderlich mapkit教程到xcode 7错误mkplacemark

时间:2015-09-18 22:49:20

标签: ios xcode swift swift2 mkplacemark

我刚刚将ray wenderlich mapkit教程升级为MKPlacemark的xcode 7错误。我仍然只是编码的新手,我不能100%确定从何处开始修复此错误。我搜索但知道有用。

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

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

错误是:

无法为类型' mkplacemark'调用初始值设定项使用列表类型参数'坐标:CLLocationCoordinate2D,addressDictionary:[String:String?])'

再次感谢,

特拉维斯。

2 个答案:

答案 0 :(得分:2)

您需要将副标题转换为AnyObject,如下所示:

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

您的“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
  }

答案 1 :(得分:1)

OK!

刚想通了我搜索得更深了!

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

所以你可以替换这一行:

let addressDictionary = [String(CNPostalAddressStreetKey): subtitle]

有了这个:

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

或者这(相当于副标题的实现):

let addressDictionary = [String(CNPostalAddressStreetKey): locationName]

感谢!!!