我正在尝试快速编写一个创建 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
:
无法将类型“[String : String?]”的值转换为预期的参数类型“[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
}
}