Swift 2 CLLocationManager Error updating
Asked Answered
J

5

20

i updated Xcode 6 to Xcode 7 beta with Swift 2. I get this error and i can't find out how to fix it, please help me. Thanks. This is my code :

 func locationManager(manager: CLLocationManager, didUpdateLocations locations: [AnyObject]) {
    let location = locations.last as! CLLocation

    let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

    self.map.setRegion(region, animated: true)
}

and i get this error :

Objective-C method 'locationManager:didUpdateLocations:' provided by method 'locationManager(_:didUpdateLocations:)' conflicts with optional requirement method 'locationManager(_:didUpdateLocations:)' in protocol 'CLLocationManagerDelegate'

Joanajoane answered 25/6, 2015 at 8:21 Comment(0)
B
39

Just had the same issue as you, change

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

to

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
Breebreech answered 25/6, 2015 at 20:22 Comment(5)
AnyObject is a supertype of CLLocation. Both should work.Nutpick
I know that but when I changed it from [AnyObject] to [CLLocation] the error went away.Breebreech
already tried that, it work but then it gave me an error on this line : let location = locations.last as! CLLocation said that locations.last can't be a CLLocationJoanajoane
i fix it by doing let location = locations.last as! CLLocation!Joanajoane
I like how, in the Stanford iOS courses, Paul Hegarty was complaining about the "AnyObject" supertype and now Apple fixed it in Swift 2. :PWorkmanship
P
2

Click on your project - go to Build Phases->Link Binary With Libraries and add CoreLocation.framework

import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

let locationManager = CLLocationManager()
var LatitudeGPS = NSString()
var LongitudeGPS = NSString()

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

func updateLocation() {
    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    //self.locationManager.distanceFilter = 10
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.startUpdatingLocation()
}

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

    locationManager.stopUpdatingLocation() // Stop Location Manager - keep here to run just once

    LatitudeGPS = String(format: "%.6f", manager.location!.coordinate.latitude)
    LongitudeGPS = String(format: "%.6f", manager.location!.coordinate.longitude)
    print("Latitude - \(LatitudeGPS)")

}

This is my code which works on xcode 7, also I did have to clean my code (Product->Clean)

Platto answered 1/7, 2015 at 22:59 Comment(0)
N
1

You need to mark your class or method with @objc attribute. So either:

@objc class MyManagerDelegate: NSObject, CLLocationManagerDelegate {

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [AnyObject]) {
        ...
    }

}

Or:

class MyManagerDelegate: CLLocationManagerDelegate {

    @objc func locationManager(manager: CLLocationManager, didUpdateLocations locations: [AnyObject]) {
        ...
    }

}
Nutpick answered 25/6, 2015 at 10:54 Comment(0)
V
0

I solved this by following code:

//CLLocationManagerDelegate
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [AnyObject]) {
    let location:CLLocation = locations[locations.count-1] as! CLLocation 

    if (location.horizontalAccuracy > 0) {
        self.locationManager.stopUpdatingLocation()
        print(location.coordinate, terminator: "")
        updateWeatherInfo(location.coordinate.latitude, longitude: location.coordinate.longitude)
    }
}
Viscid answered 9/9, 2015 at 9:6 Comment(0)
K
-1
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations.last as! CLLocation!
    manager.stopUpdatingLocation()
    let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude), span: MKCoordinateSpan(latitudeDelta: 0.001, longitudeDelta: 0.001))
    mapView.setRegion(region, animated: true)
}
Kantor answered 19/3, 2016 at 4:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.