出于某种原因,Xcode 认为我没有实现 didFailWithError 协议(protocol)的 CLLocationManagerDelegate 方法
ios - 代表必须响应 locationManager :didFailWithError: even though implemented didFailWithError method-LMLPHP
ios - 代表必须响应 locationManager :didFailWithError: even though implemented didFailWithError method-LMLPHP
我看不出我做错了什么,因为我是从另一个 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)")
        }

    }

最佳答案

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

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

关于ios - 代表必须响应 locationManager :didFailWithError: even though implemented didFailWithError method,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43578473/

10-12 06:17