Search code examples
swift2cllocationxcode7-beta4

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


I am stumped on how to initializer this placemark within my reverseGeocodeLocation attempt.

First, I do not know if the reverseGeocodeLocation is initialized correctly. It was giving me some issues; but Xcode seems to be okay with it at this point.

The real issue comes at line 55: placemark = CLPlacemark(placemark: placemarkArr[0] as! CLPlacemark)

I receive the error: "Cannot invoke initializer for type 'CLPlacemark' with an argument list of type '(place mark: CLPlacemark)'"

I am using Xcode 7 Beta 4; so I don't know if this could be the issue. I have seen older examples on how to write this line of code, but they all appear to be earlier code editions.

Thanks for any help or suggestions! This one has been stumping me for a while.

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.
    }




}

Solution

  • I am no longer having this issue. My code may be slightly different than above, so I have pasted below the portion of my code initializing the 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)
        }
    }