反向地理编码位置:"Cannot invoke initializer for type 'CLPlacemark' with an argument list of type '(placemark: CLPlacemark)'"

reverseGeocodeLocation: "Cannot invoke initializer for type 'CLPlacemark' with an argument list of type '(placemark: CLPlacemark)'"

我对如何在我的 reverseGeocodeLocation 尝试中初始化这个地标感到困惑。

首先,不知道是否正确初始化了reverseGeocodeLocation。它给了我一些问题;但 Xcode 目前似乎还可以。

真正的问题出现在第 55 行: placemark = CLPlacemark(placemark: placemarkArr[0] as!CLPlacemark)

我收到错误:"Cannot invoke initializer for type 'CLPlacemark' with an argument list of type '(place mark: CLPlacemark)'"

我正在使用 Xcode 7 Beta 4;所以我不知道这是否是问题所在。我看过有关如何编写这行代码的较早示例,但它们似乎都是较早的代码版本。

感谢您的帮助或建议!这个问题困扰了我一段时间。

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate{

    var manager:CLLocationManager?

    @IBOutlet weak var latitude: UILabel!
    @IBOutlet weak var longitude: UILabel!
    @IBOutlet weak var altitude: UILabel!
    @IBOutlet weak var course: UILabel!
    @IBOutlet weak var speed: UILabel!
    @IBOutlet weak var address: UILabel!


    override func viewDidLoad() {
        super.viewDidLoad()

        manager = CLLocationManager()
        manager!.delegate = self
        manager!.desiredAccuracy - kCLLocationAccuracyBest
        manager!.requestWhenInUseAuthorization()
        manager!.startUpdatingLocation()

    }


    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        print(locations)
        let userLocation:CLLocation = locations[0]
        self.latitude.text = "\(userLocation.coordinate.latitude)"
        self.longitude.text = "\(userLocation.coordinate.longitude)"
        self.course.text = "\(userLocation.course)"
        self.speed.text = "\(userLocation.speed)"
        self.altitude.text = "\(userLocation.altitude)"

        let geocoder = CLGeocoder()

        geocoder.reverseGeocodeLocation(userLocation) { (placemarkArr, error) -> Void in

            var placemark:CLPlacemark!
            //let gp = userLocation

            if error == nil && placemarkArr!.count > 0 {
                print(userLocation)

                placemark = CLPlacemark(placemark: placemarkArr[0] as! CLPlacemark)

                var addressString: String = ""
                if placemark.ISOcountryCode == "TW" {
                    if placemark.country != nil {
                    addressString = placemark.country!
                    }
                    if placemark.subAdministrativeArea != nil {
                    addressString = addressString + placemark.subAdministrativeArea! + ", "
                    }
                    if placemark.postalCode != nil {
                        addressString = addressString + placemark.postalCode! + " "
                    }
                    if placemark.locality != nil {
                        addressString = addressString + placemark.locality!
                    }
                }
            }
        }
    }

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




}

我不再遇到这个问题。我的代码可能与上面的代码略有不同,因此我将初始化 reverseGeocodeLocation 的代码部分粘贴在下方:

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate{

let locationManager = CLLocationManager()

var longitude: Double!
var latitude: Double!

var thoroughfare = "unknown"
var subLocality = "unknown"
var subAdministrativeArea = "unknown"
var postCode = "unknown"
var locality = "unknown"

override func viewDidLoad() {
    super.viewDidLoad()

    self.locationManager.requestAlwaysAuthorization()
    self.locationManager.requestWhenInUseAuthorization()

    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.startUpdatingLocation()
    }

}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    latitude = userLocation.coordinate.latitude
    longitude = userLocation.coordinate.longitude

    let userLocation:CLLocation = locations[0]

    CLGeocoder().reverseGeocodeLocation(userLocation) { (placemarks, error) -> Void in

        if(error == nil) {
            let loc = placemarks![0]

            self.thoroughfare = String!(loc.thoroughfare)
            self.subLocality = String!(loc.subLocality)
            self.subAdministrativeArea = String!(loc.subAdministrativeArea)
            self.postCode = String!(loc.postalCode)
            self.locality = String!(loc.locality)
    } else {
            //print(error)
        }
        print("thoroughfare: " + self.thoroughfare)
    }
}