Swift 2.0 中的 openMapsWithItems 无法使用参数列表问题调用
openMapsWithItems cannot Invoke with argument list issue in Swift 2.0
我在将我的 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])
}
有什么想法吗?谢谢
正如在the pre-release developer resources for MKMapItem中所见,openMapsWithItems:launchOptions:
现在已经从取一个[NSObject : AnyObject]!
变为取一个[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
我在将我的 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])
}
有什么想法吗?谢谢
正如在the pre-release developer resources for MKMapItem中所见,openMapsWithItems:launchOptions:
现在已经从取一个[NSObject : AnyObject]!
变为取一个[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