3

For some reason Xcode thinks I'm not implementing didFailWithError method of the CLLocationManagerDelegate protocol

enter image description here enter image description here

I fail to see what I'm doing wrong, as I literally copied from another SO post that said this didFailWithErrormethod was updated for Swift 3. So I don't understand why Xcode thinks I'm not implementing didFailWithError

Any insight would be greatly appreciated!

Code

class OptionsViewController: UIViewController,
    CLLocationManagerDelegate {
var locationManager: CLLocationManager = CLLocationManager()

    override func viewDidLoad() {
//Ask user for location
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        
        //Use users current location if no starting point set
        if CLLocationManager.locationServicesEnabled() {
            if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse
                || CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways {
                locationManager.requestLocation()
            }
            else{
                locationManager.requestWhenInUseAuthorization()
            }
        }
        else{
            //Alert user to open location service, bra bra bra here...
        }
    }

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("error:: \(error)")
    }
    
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        print("didChangeAuthorization")
        if status == CLAuthorizationStatus.authorizedWhenInUse
            || status == CLAuthorizationStatus.authorizedAlways {
            locationManager.requestLocation()
        }
        else{
            //other procedures when location service is not permitted.
        }
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        print("Did update location called")
//        let locValue:CLLocationCoordinate2D = manager.location!.coordinate
//        print("locations = \(locValue.latitude) \(locValue.longitude)")
        if locations.first != nil {
            print("location:: (location)")
        }
        
    }
Community
  • 1
  • 1
14wml
  • 4,048
  • 11
  • 49
  • 97
  • where is locationManager declared? – Leo Dabus Apr 24 '17 at 01:57
  • 1
    The picture you posted clearly tells you how to fix the problem. – rmaddy Apr 24 '17 at 02:06
  • The code works with the exception of two issues, 1) I don't see any declaration of your `locationManager` var 2) Your missing a final `}` for your class. With those two fixes, it compiles for for me. – rmp Apr 24 '17 at 21:30
  • @LeoDabus I was a bit hasty and did not add the declaration by mistake. I've added it now. Please let me know if my declaration is correct – 14wml Apr 24 '17 at 22:09
  • @rmp I was a bit hasty and did not add the declaration by mistake. I've added it now. Please let me know if my declaration is correct. Also I know I'm definitely not missing `}` in my actual code b/c my code compiles; it's just the `CLLManager` does not work – 14wml Apr 24 '17 at 22:09
  • @rmaddy could you please copy and past the alternative function header I should type instead of the one I have in the code? I don't see what the difference is between what I have in my code and what Xcode is suggesting I use. – 14wml Apr 24 '17 at 22:11
  • Did you do what Xcode suggested and make the method `private`? – rmaddy Apr 24 '17 at 22:12
  • @rmaddy Yep didn't do anything. Still got error `Delegate must respond to locationManager:didFailWithError:` – 14wml Apr 25 '17 at 01:34

1 Answers1

8

Aaaandd I realized it's b/c I needed to use an Error of a specific type (specifically Swift.Error) This is the right method declaration for didFailWithError:

func locationManager(_ manager: CLLocationManager, didFailWithError error: Swift.Error) {
14wml
  • 4,048
  • 11
  • 49
  • 97
  • This was my issue too. In my codebase, someone else had declared an `enum` called Error and it was clashing with the `Swift.Error`. – Isuru Jun 03 '17 at 08:27