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

1.4k Views Asked by At

I am getting two errors with this code:

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

and

"Cannot invoke 'geocodeAddressstring' with an argument list of type '(String, completionHandler: ([AnyObject?]!, NSError?) -> _)'

I tried to put optionals here and there having read that it might fix things, but didn't work.

What should I change to my code to fix these errors and make my ViewController work?

import UIKit
import CoreLocation
import AddressBook
import Contacts
import MapKit

class ViewController: UIViewController {

    @IBOutlet weak var address: UITextField!
    @IBOutlet weak var city: UITextField!
    @IBOutlet weak var province: UITextField!
    @IBOutlet weak var zip: UITextField!

    var coords: CLLocationCoordinate2D?

    @IBAction func getDirections(sender: UIButton) {

        let geoCoder = CLGeocoder()

        let addressString = "\(address.text) \(city.text) \(province.text) \(zip.text)"

        geoCoder.geocodeAddressString(addressString, completionHandler: {(placemarks: [AnyObject?]!, error: NSError?) in

            if error != nil {

                print("Geocode failed with error: \(error?.localizedDescription)")

            } else if placemarks?.count > 0 {

                let placemark = placemarks[0] as! CLPlacemark
                let location = placemark.location
                self.coords = location.coordinate

                self.showMap()
            }
        })
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    func showMap() {
        let addressDict =[kABPersonAddressStreetKey as NSString: address.text!,
            kABPersonAddressCityKey: city.text!,
            kABPersonAddressStateKey: province.text!,
            kABPersonAddressZIPKey: zip.text! ]

        let place = MKPlacemark(coordinate: coords!, addressDictionary: addressDict)
        let mapItem = MKMapItem(placemark: place)
        let options = [MKLaunchOptionsDirectionsModeDriving]

        mapItem.openInMapsWithLaunchOptions(options)
    }
}
2

There are 2 best solutions below

2
On

Have you upgrade to swift 2.0?

Swift 1.2 and before

geoCoder.geocodeAddressString(addressString, completionHandler: {(placemarks: [AnyObject?]!, error: NSError?) in

Swift 2.0

geocoder.geocodeAddressString(addressString, completionHandler: {(placemarks: [CLPlacemark]?, error: NSError?) -> Void in

And also change

let placemark = placemarks[0] as! CLPlacemark

To

let placemark = placemarks[0]
0
On

For the continued MKPlacemark error, try changing

let place = MKPlacemark(coordinate: coords!, addressDictionary: addressDict)

to

let place = MKPlacemark.init(coordinate: coords!, addressDictionary: (addressDict as! [String:AnyObject]))