Skip to content

sachinkesiraju/SKObserver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

SKObserver

Every iOS dev makes a KVO wrapper at some point, here's mine.
SKObserver is an NSObject category that provides a cleaner block based syntax for observing key value changes.

Setup

Simply drag and drop the NSObject+SKObserver files into your project and `#import "NSObject+SKObserver.h"` and you're 💯.

Implementation

To start observing changes to an object, call the method `sk_addObserverForKeyPath:withBlock:` on your object as follows. The block will return a dictionary containing the changes on the object. ``` [object sk_addObserverForKeyPath:@"keyPath" withBlock:^(NSDictionary *change) { NSLog(@"Change %@", change); }]; ```

To stop all observers from listening to changes on the object

[object sk_removeAllObservers];

In addition, to remove a specfic observer on the object

id observer = [object sk_addObserverForKeyPath:@"keyPath" withBlock:^(NSDictionary <NSKeyValueChangeKey, id>  *change)
{
   NSLog(@"Change %@", change);
}];
//when done observing
[object sk_removeObserver:observer];

And that's it! No more long and messy 'observeValueForKeyPath' methods.

Community

If you found an issue or would like to improve SKObserver feel free to raise an issue/submit a PR. You can also reach out to me on Twitter @_sachink

For reference on how KVO works, I encourage you to check out this post before diving into this library.

License

SKObserver is available under the MIT License. Check out the LICENSE for more information.

About

Block based kvo wrapper

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published