【问题标题】:Cannot find an initializer for type 'MKPlacemark' that accepts an argument list of type找不到接受类型参数列表的“MKPlacemark”类型的初始化程序
【发布时间】:2015-07-03 19:40:07
【问题描述】:

找不到接受(coordinate: CLLocationCoordinate2D, addressDictionary: [String: String?]) 类型参数列表的MKPlacemark 类型的初始化程序。

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

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

    }
}

【问题讨论】:

    标签: ios swift


    【解决方案1】:

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

    所以你可以替换这一行:

        let addressDictionary = [String(CNPostalAddressStreetKey): subtitle]
    

    有了这个:

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

    或者这个(相当于实现了字幕):

        let addressDictionary = [String(CNPostalAddressStreetKey): locationName]
    

    【讨论】:

      【解决方案2】:

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

      let addressDictionary = [String(CNPostalAddressStreetKey): subtitle]
      

      用这个:

      let addressDictionary = [CNPostalAddressStreetKey : subtitle!]
      

      【讨论】:

        猜你喜欢
        • 2015-09-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多