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.
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
to
and the last line
to
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 avoidNSDictionary
, please use Swift'sDictionary