Ios MKPlacemark上的参数类型错误
我试图在swift中编写一个创建mkmaItem的函数,但出现了一个字符串错误。代码如下:Ios MKPlacemark上的参数类型错误,ios,swift,mkplacemark,Ios,Swift,Mkplacemark,我试图在swift中编写一个创建mkmaItem的函数,但出现了一个字符串错误。代码如下: func mapItem() -> MKMapItem { let addressDictionary = [String(kABPersonAddressStreetKey): subtitle] let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: addressDictionary)
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
}
}
您的subtitle属性看起来像一个可选字符串,但是
MKPlacemark
初始值设定项需要一个[String:AnyObject]?
类型的参数,用于addressDictionary
这是什么意思
预期的参数类型是一个字典,其中键是字符串
,值是AnyObject
,因此它可以是任何类型。除nil值以外的任何值!但是subtitle
属性可以是nil,因此出现此错误
在使用值之前先将其展开:
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
为nil,您还可以返回可选的MKMapItem
对象。选项由您选择;)替换此字符串:
let title: String?
替换此代码:
var subtitle: String? {
return locationName
}
您需要将字幕转换为任意对象,如下所示:
let addressDict = [String(kABPersonAddressStreetKey): self.subtitle as! AnyObject]
“func mapItem()->MKMapItem{}”的完整代码为:
谢谢你的回复!Tho,当我尝试时,他在if let subtitle=subtitle上给了我一个错误,说“条件绑定中的绑定值必须是可选类型,所以我只是删除了if条件,结果一切正常。之前的错误看起来像subtitle是非可选的,但可能是隐式的未包装可选。出于好奇,这个变量的声明是什么?不管怎样,我很高兴知道您解决了您的问题:)您的代码即使在方法的第一个版本中也可以正常工作。如果您查看您发布的第一个代码()的屏幕截图,您可以看到subtitle是一个可选字符串,因此它确认了对该问题的第一个解释:)
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
}