找不到类型' MKPlacemark'的初始值设定项。接受类型的参数列表

时间:2015-07-03 19:40:08

标签: ios swift

无法找到接受类型为MKPlacemark的参数列表的类型(coordinate: CLLocationCoordinate2D, addressDictionary: [String: String?])的初始值设定项。

我不知道如何解决这个问题,请帮忙。

import Foundation
import MapKit
import AddressBook
import Contacts

class Artwork: NSObject, MKAnnotation {
    let title: String?
    let locationName: String
    let discipline: String
    let coordinate: CLLocationCoordinate2D

    init(title: String, locationName: String, discipline: String, coordinate: CLLocationCoordinate2D) {
        self.title = title
        self.locationName = locationName
        self.discipline = discipline
        self.coordinate = coordinate

        super.init()
    }


    var subtitle: String? {
        return locationName

    }


    func mapItem() -> MKMapItem {

        let addressDictionary = [String(CNPostalAddressStreetKey): subtitle]
        let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: addressDictionary)

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

        return mapItem

    }
}

2 个答案:

答案 0 :(得分:3)

问题是locationName是可选的,因此推断addressDictionary的类型为[String:String?],它与初始化程序不兼容。但是[String:String]类型的字典可以工作。

所以你可以替换这一行:

    let addressDictionary = [String(CNPostalAddressStreetKey): subtitle]

有了这个:

    let addressDictionary = [String(CNPostalAddressStreetKey): subtitle!]

或者这(相当于副标题的实现):

    let addressDictionary = [String(CNPostalAddressStreetKey): locationName]

答案 1 :(得分:0)

CNPostalAddressStreetKey是一个字符串。 所以你可以替换这一行:

let addressDictionary = [String(CNPostalAddressStreetKey): subtitle]

用这个:

let addressDictionary = [CNPostalAddressStreetKey : subtitle!]