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

时间:2015-07-01 16:56:05

标签: ios xcode swift mapkit xcode7-beta2

我在使用此代码时遇到两个错误:

  

"找不到类型' MKPlacemark'的初始值设定项接受类型'的参数列表(坐标:CLLocationCoordinate2D,addressDictionary:[NSString:String])'

  

"无法调用' geocodeAddressstring'使用类型'的参数列表(String,completionHandler:([AnyObject?]!,NSError?) - > _)'

我试图把选项放在这里,那里已经读过它可以解决问题,但是没有用。

我应该更改我的代码以修复这些错误并使我的ViewController工作?

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 个答案:

答案 0 :(得分:3)

你升级到swift 2.0了吗?

Swift 1.2及之前

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

Swift 2.0

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

并且还要改变

let placemark = placemarks[0] as! CLPlacemark

let placemark = placemarks[0]

答案 1 :(得分:-1)

对于持续的MKPlacemark错误,请尝试更改

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

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