TestsTested | ✓ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Dec 2014 |
Maintained by Unclaimed.
KISObserver is an easy way to use the Key-Value Observing (KVO) pattern.
You can observe objects for keypaths with blocks, selectors and the usual API. Moreover, the observer removes automagically the observations when it is deallocated.
An observation with a block and the default options:
[self observeObject:ob forKeyPaths:@"property" withBlock:^(KISNotification *notification) {
NSLog(@"New value:\t%@", notification.newValue);
NSLog(@"Old value:\t%@", notification.oldValue);
}];
An observation with a selector on a one-to-many relation:
NSKeyValueObservingOptions opt = NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld;
[self observeObject:ob forKeyPaths:@"arr" options:opt withSelector:@selector(onChangeWithNotification:)];
// ...
- (void)onChangeWithNotification:(KISNotification *)notification
{
NSLog(@"isSetting:\t%hhd", notification.isSetting);
NSLog(@"Insertions:\t%@", notification.insertionIndexSet);
NSLog(@"Removals:\t%@", notification.removalIndexSet);
NSLog(@"Replacements:\t%@", notification.replacementIndexSet);
}
An observation of many keypaths at the same time:
[self observeObject:ob forKeyPaths:@"prop1|prop2" withBlock:^(KISNotification *notification) {
NSLog(@"KeyPath:\t%@", notification.keyPath);
NSLog(@"New value:\t%@", notification.newValue);
NSLog(@"Old value:\t%@", notification.oldValue);
}];
Your dealloc:
- (void)dealloc
{
// Nothing to do. :-)
}
Use CocoaPods!
CocoaPods is a dependency manager for Objective-C, which automates and simplifies the process of using 3rd-party libraries in your projects.
Add the project inside your Podfile
pod 'KISObserver', '~> 1.0.x'
Update your installation
pod install
Import KISObserver within the files that are going to use it
#import <KISObserver/KISObserver.h>
KISObserver is available under the MIT license. See the LICENSE file for more info.