Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KVO / Exception handling in Swift

Tags:

ios

swift

Please note that this question was written in july 2014, before swift 1.0 when I mostly ignored anything about swift and tried to "translate" code from objC to swift. This is not a good solution and I now know better. KVO is something we love in ObjC, but I strongly recommend not to use it in swift and explore some alternative in swift. http://blog.scottlogic.com/2015/02/11/swift-kvo-alternatives.html. Remember: if something is hard to do, then maybe it is not meant to be done.

As an obj-C dev, I've been used to KVO for, well, year, and one of the recurring problem is the potential unsafe of calling removeObserver:forKeyPath: I usually surround this with a @try...@catch clause... Now with swift, I haven't find yet the try ... catch thingy :) Any leads on how to workaround the problem?

Cheers

Here is a 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);

}
like image 948
Florian Burel Avatar asked Jun 06 '14 08:06

Florian Burel


People also ask

How does KVO work in Swift?

KVO, which stands for Key-Value Observing, is one of the techniques for observing the program state changes available in Objective-C and Swift. The concept is simple: when we have an object with some instance variables, KVO allows other objects to establish surveillance on changes for any of those instance variables.

What is KVO and KVC in IOS Swift?

KVO and KVC or Key-Value Observing and Key-Value Coding are mechanisms originally built and provided by Objective-C that allows us to locate and interact with the underlying properties of a class that inherits NSObject at runtime.

What is exception handling in Swift?

In this tutorial, we will learn to handle Swift errors with the help of examples. An error (exception) is an unexpected event that occurs during program execution. For example, var numerator = 10 var denominator = 0 // try to divide a number by 0 var result = numerator / denominator // error code.

Do try catch block in Swift?

The try/catch syntax was added in Swift 2.0 to make exception handling clearer and safer. It's made up of three parts: do starts a block of code that might fail, catch is where execution gets transferred if any errors occur, and any function calls that might fail need to be called using try .


1 Answers

As the other answer says, there's no try-catch in Swift yet. Honestly, I would discourage the use of try/catch in the scenario you mention. This can easily be solved by keeping track of your object's state, which is always a good thing in object oriented programming.

like image 82
xtravar Avatar answered Oct 11 '22 15:10

xtravar