【发布时间】:2025-12-07 23:20:13
【问题描述】:
我在将 Swift 1.2 代码转换为 2.0 时遇到了一些问题 - 这是其中之一。
我有一个功能,它可以打开 iOS 地图应用程序以指示前往某个位置的方向。在转换之前它工作正常。现在我收到以下错误消息:
Cannot invoke 'openMapsWithItems' with an argument list of type '([MKMapItem], launchOptions: [NSObject : AnyObject])'
这是我的代码(错误出现在最后一行):
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])
}
有什么想法吗?谢谢。
【问题讨论】:
-
声明
launchOptionslikelet launchOptions : [NSObject : AnyObject] = [MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving]并删除as [NSObject : AnyObject] -
感谢 Kametrixom,但我仍然收到以下错误:“无法使用类型参数列表调用 openMapsWithItems ([MKMapItem],launchOptions: [NSObject : AnyObject])”。我已经清理了构建以防出现问题,但它仍然无法正常工作。
标签: swift apple-maps swift2