3

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

在此处输入图像描述 在此处输入图像描述

我看不到我做错了什么,因为我从另一个SO 帖子中复制了该帖子,该帖子说此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)")
        }
        
    }
4

1 回答 1

7

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

func locationManager(_ manager: CLLocationManager, didFailWithError error: Swift.Error) {
于 2017-04-25T01:39:09.797 回答