委托必须响应locationManager:didFailWithError:即使实现了didFailWithError方法

时间:2017-04-24 01:34:06

标签: ios xcode swift3 cllocationmanager didfailwitherror

出于某种原因,Xcode认为我没有实现didFailWithError协议的CLLocationManagerDelegate方法

enter image description here enter image description here

我没有看到我做错了什么,因为我真的从另一个SO post复制了说这个didFailWithError方法已经为Swift 3更新了。所以我不明白为什么Xcode认为我我没有实施didFailWithError

非常感谢任何见解!

代码

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)")
        }

    }

1 个答案:

答案 0 :(得分:6)

Aaaandd我意识到这是b / c我需要使用特定类型的Error(特别是Swift.Error) 这是didFailWithError的正确方法声明:

func locationManager(_ manager: CLLocationManager, didFailWithError error: Swift.Error) {