gpt4 book ai didi

swift - openMapsWithItems 无法调用 Swift 2.0 中的参数列表问题

转载 作者:行者123 更新时间:2023-11-28 07:05:14 24 4
gpt4 key购买 nike

我在将我的 Swift 1.2 代码转换为 2.0 时遇到了一些问题 - 这是其中一个问题。

我有一个功能可以打开 iOS map 应用程序来指示某个位置。在转换之前它工作正常。现在我收到以下错误消息:

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 - openMapsWithItems 无法调用 Swift 2.0 中的参数列表问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30826594/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com