gpt4 book ai didi

ios - MKPlacemark 上的参数类型错误

转载 作者:搜寻专家 更新时间:2023-10-31 22:21:08 25 4
gpt4 key购买 nike

我正在尝试快速编写一个创建 MKMapItem 的函数,但我收到一个字符串错误。这是代码:

func mapItem() -> MKMapItem {
let addressDictionary = [String(kABPersonAddressStreetKey): subtitle]
let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: addressDictionary)

let mapItem = MKMapItem(placemark: placemark)
mapItem.name = title

return mapItem
}

当我尝试创建 placemark 时出现以下错误:

Cannot convert value of type "[String : String?]" to expected argument type "[String : AnyObject]?

完整类代码:

class Bar: NSObject, MKAnnotation {

// MARK: Properties
let id: Int
let title: String
let locationName: String
let url: String
let imageUrl: String
let tags: String
let coordinate: CLLocationCoordinate2D

// MARK: Initialisation
init(id: Int, adress: String, name: String, url: String, tags: String, imageUrl: String, coordinate: CLLocationCoordinate2D) {
// Affectation des attributs
self.id = id
self.title = name
self.locationName = adress
self.url = url
self.imageUrl = imageUrl
self.tags = tags
self.coordinate = coordinate
}

// MARK: Subtitle

var subtitle: String {
return locationName
}

// MARK: Helper

func mapItem() -> MKMapItem {
var addressDictionary : [String:String]?
addressDictionary = [String(kABPersonAddressStreetKey): subtitle]

let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: addressDictionary)

let mapItem = MKMapItem(placemark: placemark)
mapItem.name = title

return mapItem
}
}

最佳答案

替换这个字符串:

  let title: String?

替换这段代码:

 var subtitle: String? {
return locationName
}

您需要将字幕转换为 AnyObject,如下所示:

let addressDict = [String(kABPersonAddressStreetKey): self.subtitle as! AnyObject]

“func mapItem() -> MKMapItem { }”的完整代码将是:

func mapItem() -> MKMapItem {
let addressDict = [String(kABPersonAddressStreetKey): self.subtitle as! AnyObject]
let placemark = MKPlacemark(coordinate: self.coordinate, addressDictionary: addressDict)

let mapItem = MKMapItem(placemark: placemark)
mapItem.name = self.title

return mapItem
}

关于ios - MKPlacemark 上的参数类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36696702/

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