openMapsWithItems cannot Invoke with argument list issue in Swift 2.0

876 views Asked by At

I'm having a few issues converting my Swift 1.2 code to 2.0 - this is one of those issues.

I have a function which opens the iOS Maps app to give directions to a location. It was working fine until the conversion. Now I get the following error message:

Cannot invoke 'openMapsWithItems' with an argument list of type '([MKMapItem], launchOptions: [NSObject : AnyObject])'

This is my code (The error appears on the last line):

func openMapsWithDirections(longitude:Double, latitude:Double, placeName:String){

    var coordinate = CLLocationCoordinate2DMake(CLLocationDegrees(longitude), CLLocationDegrees(latitude))
    var placemark:MKPlacemark = MKPlacemark(coordinate: coordinate, addressDictionary:nil)
    var mapItem:MKMapItem = MKMapItem(placemark: placemark)
    mapItem.name = placeName
    let launchOptions:NSDictionary = NSDictionary(object: MKLaunchOptionsDirectionsModeDriving, forKey: MKLaunchOptionsDirectionsModeKey)
    var currentLocationMapItem:MKMapItem = MKMapItem.mapItemForCurrentLocation()

    MKMapItem.openMapsWithItems([currentLocationMapItem, mapItem], launchOptions: launchOptions as [NSObject : AnyObject])
}

Any ideas? Thanks.

1

There are 1 answers

1
vrwim On BEST ANSWER

As can be seen in the pre-release developer resources for MKMapItem, openMapsWithItems:launchOptions: now has changed from taking a [NSObject : AnyObject]! to taking a [String : AnyObject]?, so you will have to declare (or cast) it as such.

Change in your code the line

let launchOptions:NSDictionary = NSDictionary(object: MKLaunchOptionsDirectionsModeDriving, forKey: MKLaunchOptionsDirectionsModeKey)

to

let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]

and the last line

MKMapItem.openMapsWithItems([currentLocationMapItem, mapItem], launchOptions: launchOptions as [NSObject : AnyObject])

to

MKMapItem.openMapsWithItems([currentLocationMapItem, mapItem], launchOptions: launchOptions)

That should work.

Sidenote: You should change your code style to allow Swift infer most types. Please stop hurting everyone's eyes with var placemark:MKPlacemark = MKPlacemark(...). Also try to avoid NSDictionary, please use Swift's Dictionary