MKPlacemark 上的参数类型错误
Argument type error on MKPlacemark
我正在尝试在 swift 中编写一个创建 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 代码:
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
}
}
您的副标题 属性 看起来像一个可选的字符串,但是 MKPlacemark
初始值设定项需要 [String : AnyObject]?
类型的参数用于 addressDictionary
。
这是什么意思?
预期的参数类型是一个字典,其中键是 String
,值是 AnyObject
,所以它可以是任何东西。除了零值之外的任何东西!但是你的 subtitle
属性 可能是零,所以你有这个错误。
在使用之前解开你的价值:
func mapItem() -> MKMapItem {
var addressDictionary : [String:String]?
if let subtitle = subtitle {
// The subtitle value used here is a String,
// so addressDictionary conforms to its [String:String] type
addressDictionary = [String(kABPersonAddressStreetKey): subtitle
}
let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: addressDictionary)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = title
return mapItem
}
如果 subtitle
为零,您还可以 return 一个可选的 MKMapItem
objects。选择权在你;)
替换此字符串:
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
}
我正在尝试在 swift 中编写一个创建 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 代码:
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
}
}
您的副标题 属性 看起来像一个可选的字符串,但是 MKPlacemark
初始值设定项需要 [String : AnyObject]?
类型的参数用于 addressDictionary
。
这是什么意思?
预期的参数类型是一个字典,其中键是 String
,值是 AnyObject
,所以它可以是任何东西。除了零值之外的任何东西!但是你的 subtitle
属性 可能是零,所以你有这个错误。
在使用之前解开你的价值:
func mapItem() -> MKMapItem {
var addressDictionary : [String:String]?
if let subtitle = subtitle {
// The subtitle value used here is a String,
// so addressDictionary conforms to its [String:String] type
addressDictionary = [String(kABPersonAddressStreetKey): subtitle
}
let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: addressDictionary)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = title
return mapItem
}
如果 subtitle
为零,您还可以 return 一个可选的 MKMapItem
objects。选择权在你;)
替换此字符串:
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
}