Jack Berstrem
Jack Berstrem

Reputation: 535

Cannot find an initializer for type 'MKPlacemark' that accepts an argument list of type

Cannot find an initializer for type MKPlacemark that accepts an argument list of type (coordinate: CLLocationCoordinate2D, addressDictionary: [String: String?]).

I have no idea how to fix this, please help.

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

    }
}

Upvotes: 1

Views: 571

Answers (2)

Walden
Walden

Reputation: 1

CNPostalAddressStreetKey is a string. So you can replace this line:

let addressDictionary = [String(CNPostalAddressStreetKey): subtitle]

with this:

let addressDictionary = [CNPostalAddressStreetKey : subtitle!]

Upvotes: 0

Clafou
Clafou

Reputation: 15400

The problem is that locationName is an optional, so addressDictionary is inferred to be of type [String:String?] which is incompatible with the initializer. But a dictionary of type [String:String] would work.

So you can replace this line:

    let addressDictionary = [String(CNPostalAddressStreetKey): subtitle]

With this:

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

Or this (which is equivalent given the implementation of subtitle):

    let addressDictionary = [String(CNPostalAddressStreetKey): locationName]

Upvotes: 3

Related Questions