1
votes

I just upgraded ray wenderlich mapkit tutorial to xcode 7 error for MKPlacemark. I'm still only new with coding and I'm not 100% sure where to start on how to fix this error. I searched but to know avail.

Thanks for any help. It's much appreciated.

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

This is the code I'm getting an error with:

// 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

The error is:

Cannot invoke initializer for type 'mkplacemark' with an argument of list type 'coordinate:CLLocationCoordinate2D, addressDictionary:[String : String?])'

Thanks again,

Travis.

2

2 Answers

2
votes

You need to cast your subtitle as AnyObject as shown below:

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

and your complete code for "func mapItem() -> MKMapItem { }" will be:

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
votes

OK!

Just figured it out I Searched deeper!

The problem is that locationName is an optional, so addressDictionary is inferred to be of type [String:String?] which is incompatible with the initializer. But a dictionary of type [String:String] would work.

So you can replace this line:

let addressDictionary = [String(CNPostalAddressStreetKey): subtitle]

With this:

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

Or this (which is equivalent given the implementation of subtitle):

let addressDictionary = [String(CNPostalAddressStreetKey): locationName]

Thanks!!!