1

我在将 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])
}

有任何想法吗?谢谢。

4

1 回答 1

1

MKMapItem 的预发布开发人员资源中可以看出openMapsWithItems:launchOptions:现在已从采用 a 更改[NSObject : AnyObject]!为采用 a [String : AnyObject]?,因此您必须这样声明(或强制转换)它。

在您的代码中更改该行

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

let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]

最后一行

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

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

那应该行得通。

旁注:您应该更改代码样式以允许 Swift 推断大多数类型。请不要再伤害大家的眼睛了var placemark:MKPlacemark = MKPlacemark(...)。也尽量避免NSDictionary,请使用 Swift 的Dictionary

于 2015-06-14T09:31:36.197 回答