I would like to retrieve the current location. I work with on swift Xcode 7. I looked PLUSIEUR tutorials, but every time they use the same method. Here is my code and my error: !
Error : Cannot subscript a value of type [CLPlacemark] with an index type int
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
let LocationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.LocationManager.delegate = self
self.LocationManager.desiredAccuracy = kCLLocationAccuracyBest
self.LocationManager.requestWhenInUseAuthorization()
self.LocationManager.startUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [AnyObject]) {
CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: { (placemarks, error) -> Void in
if (error != nil) {
print("Error")
return
}
if placemarks!.count > 0 {
let pm = placemarks[0] as CLPlacemark
self.displayLocationInfo(pm)
}
else {
print("errorData")
}
})
}
func displayLocationInfo(placemark: CLPlacemark){
self.LocationManager.stopUpdatingLocation()
print(placemark.locality)
print(placemark.postalCode)
print(placemark.administrativeArea)
print(placemark.country)
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("Error:" + error.localizedDescription)
}
}