MKPlacemark上的参数类型错误

时间:2016-04-18 14:22:08

标签: ios swift mkplacemark

我正在尝试在swift中编写一个创建MKMapItem的函数,但是我收到了一个String错误。这是代码:

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
    }    
}

2 个答案:

答案 0 :(得分:3)

替换此字符串:

  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
  }

答案 1 :(得分:1)

您的字幕属性看起来像一个可选字符串,但<header> <div id="socialm"> <a href="https://www.facebook.com" target="_blank"> <img src="http://placekitten.com.s3.amazonaws.com/homepage-samples/96/140.jpg" width="45px" height="45px" /> </a> <a href="https://www.instagram.com" target="_blank"> <img src="http://placekitten.com.s3.amazonaws.com/homepage-samples/96/140.jpg" width="45px" height="45px" /> </a> <a href="https://www.youtube.com/channel/..." target="_blank"> <img src="http://placekitten.com.s3.amazonaws.com/homepage-samples/96/140.jpg" width="45px" height="45px" /> </a> </div> <h1>Photography and visual arts</h1> </header>初始值设定项需要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 } 为零,您还可以返回可选的MKMapItem个对象。选择是你的;)