Note that this question was written in July 2014, before quickly 1.0, when I basically ignored anything about Swift and tried to βtranslateβ the code from objC to swift. This is a bad decision, and now I know better. KVO is what we love about ObjC, but I highly recommend not using it in the fast and exploring the alternative quickly. http://blog.scottlogic.com/2015/02/11/swift-kvo-alternatives.html . Remember: if something is difficult to do, then perhaps this does not mean to be done.
As an obj-C developer, Iβm used to KVO, well, a year, and one of the recurring problems is the potentially dangerous call to removeObserver:forKeyPath: I usually surround this with the sentence @ try...@catch ... Now with a quick one, I haven't found it yet try ... to catch a thing :) Any conclusions on how to solve the problem?
Greetings
Here is an example of what I mean
override func viewDidLoad() { super.viewDidLoad(); self.summaryTextView.text = self.show?.overview; self.title = self.show?.title; if(self.show?.imageData) { self.posterImageView.image = UIImage(data: self.show?.imageData); } else { self.posterImageView.image = UIImage(named:"wait"); show?.addObserver(self, forKeyPath: "imageData", options: NSKeyValueObservingOptions.New, context: nil); } } override func viewDidDisappear(animated: Bool) { // Will crash if self was not a observer in the first place self.show?.removeObserver(self, forKeyPath:"imageData"); } override func observeValueForKeyPath(keyPath: String!, ofObject object: AnyObject!, change: NSDictionary!, context: CMutableVoidPointer) { self.posterImageView.image = UIImage(data: self.show?.imageData); }
source share