Próbuję zaimplementować wymagania protokołu CLLocationManagerDelegate
za pośrednictwem rozszerzenia protokołu, ale menedżer lokalizacji nie widzi go w rozszerzeniu protokołu i kończy się niepowodzeniem. Jednak po przeniesieniu do klasy działa z tym samym kodem.Rozszerzenia protokołu nie mogą spełniać zgodności CLLocationManagerDelegate?
Oto co robię:
class ViewController: UIViewController, MyLocationProtocol {
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers
locationManager.distanceFilter = 1000.0
locationManager.delegate = self
// Below crashes when implementation in protocol extension
locationManager.requestLocation()
}
}
protocol MyLocationProtocol: CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
}
extension MyLocationProtocol /*where Self: UIViewControll*/ { // Tried using where clause but still no go :(
// Not being triggered by CLLocationManagerDelegate! :(
// Move to ViewController class and error goes away
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("MyLocationProtocol: locationManager: didUpdateLocations")
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("MyLocationProtocol: locationManager: didFailWithError")
}
}
Wskazówki w extension MyLocationProtocol
Kładę implementacje didUpdateLocations
i didFailWithError
tam. Nigdy się nie uruchamiają i nie rozbijają, mówiąc: 'Delegate must respond to locationManager:didUpdateLocations:'
. Jeśli przeniesię ten sam kod didUpdateLocations
i didFailWithError
do ViewController
, wszystko będzie działać zgodnie z oczekiwaniami.
Czy jest coś, czego mi brakuje, dlaczego nie działa przez rozszerzenia protokołu? Ta klasa jest uznawana za zgodną z CLLocationManagerDelegate
, w przeciwnym razie może zawieść pod adresem locationManager.delegate = self
. Jakieś pomysły na to, jak to działa, czy gdzieś tam jest błąd?
gdzie jest funkcja requestLocation() z? – user3441734
Nowe API iOS 9. – TruMan1