Latest 1.0.0
Homepage https://github.com/SomeRandomiOSDev/KeyValueObservation
License MIT
Platforms ios 8.0, requires ARC
Frameworks Foundation
Authors

Block based key-value observations in Objective-C

Version
License
Platform

Installation

KeyValueObservation is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'KeyValueObservation'

Usage

First import KeyValueObservation at the top of your Objective-C source file:

@import KeyValueObservation;

Then, adding an observer block is as simple as:

NSString *keyPath = @"Some Key Path";
NSKeyValueObservingOptions options = ...

NSObject *observation = [object observeKeyPath:keyPath 
                                       options:options 
                                 changeHandler:^(id object, SRDKeyValueObservedChange *change) {

    NSLog(@"Old value = %@", [change.oldValue description]);
    NSLog(@"New value = %@", [change.newValue description]);
    ...
}];

The Key-Value Observation remains in affect as long as the returned observation object remains alive. Once this object is deallocated the observation block is released and no longer responds to changes. Optionally, you could also call the invalidate method of the returned object to stop observing changes before the object is deallocated.

Like regular Foundation’s KVO APIs, NSArray objects aren’t directly observable using KVO. However, like the Foundation provided APIs, you can observe the changes for objects within the array. For example:

NSString *keyPath = @"Some Key Path";
NSIndexSet *indexes = ...
NSKeyValueObservingOptions options = ...

NSObject *observation = [array observeKeyPath:keyPath
                          forObjectsAtIndexes:indexes
                                      options:options 
                                changeHandler:^(id object, SRDKeyValueObservedChange *change) {

    NSIndexSet *changedIndexes = change.indexes;

    // oldValue and newValue are arrays containing the before and after values of the
    // objects at the `changedIndexes`
    NSLog(@"Old value = %@", [change.oldValue description]);
    NSLog(@"New value = %@", [change.newValue description]);

    ...
}];

Author

Joseph Newton, [email protected]

License

KeyValueObservation is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "KeyValueObservation",
    "version": "1.0.0",
    "summary": "A small KVO helper library",
    "description": "A small KVO helper library that provides a NSObject and a NSArray category for observing key value changes using blocks",
    "homepage": "https://github.com/SomeRandomiOSDev/KeyValueObservation",
    "license": "MIT",
    "authors": {
        "Joseph Newton": "[email protected]"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source": {
        "git": "https://github.com/SomeRandomiOSDev/KeyValueObservation.git",
        "tag": "1.0.0"
    },
    "public_header_files": [
        "KeyValueObservation/Classes/NSObject+KeyValueObservation.h",
        "KeyValueObservation/Classes/NSArray+KeyValueObservation.h",
        "KeyValueObservation/Classes/SRDKeyValueObservation.h",
        "KeyValueObservation/Classes/SRDKeyValueObservedChange.h"
    ],
    "source_files": "KeyValueObservation/Classes/**/*",
    "frameworks": "Foundation",
    "requires_arc": true
}

Pin It on Pinterest

Share This