I want to remove the observer after it starts or when the view has disappeared. Here is the code, but sometimes the observer was already deleted when I want to delete it again. How to check if it is still registered?
override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) { if(!didOnce){ if(keyPath == "myLocation"){ location = mapView.myLocation.coordinate; self.mapView.animateToLocation(self.location!); self.mapView.animateToZoom(15); didOnce = true; self.mapView.removeObserver(self, forKeyPath: "myLocation"); } } } override func viewDidAppear(animated: Bool) { didOnce = false; } override func viewWillDisappear(animated: Bool) { if(!didOnce){ self.mapView.removeObserver(self, forKeyPath: "myLocation"); didOnce = true; } }
ios swift
Megax
source share