Coder Social home page Coder Social logo

kvo-block-binding's Introduction

Bind blocks to properties using KVO

This repo contains a simple category, NSObject+WSObservation, and an example project. The category adds methods for observing a keypath using a block:

- (WSObservationBinding*)observe:(id)object 
                         keyPath:(NSString *)keyPath
                         options:(NSKeyValueObservingOptions)options 
                           block:(WSObservationBlock)block;

- (WSObservationBinding*)observe:(id)object 
                         keyPath:(NSString *)keyPath
                           block:(WSObservationBlock)block;

The observer is the receiver of the message. The object is the object to observe. The keyPath is the standard ObjectiveC of referencing properties within an object tree (e.g. @"some.path.to.property"). The options are the same as you would pass for the traditional KVO methods. The second method simply calls the first with the default observing options of NSKeyValueObservingOptionNew & NSKeyValueObservingOptionOld

To remove the bindings, there are three methods:

This will remove all observations on the specified object for the receiver of the message:

- (void)removeAllObservationsOn:(id)object;

This will remove all observations on the receiver on a specified object for a particular keypath:

- (void)removeAllObserverationsOn:(id)object keyPath:(NSString*)keyPath;

And this will remove ALL block-based observations on the receiver

- (void)removeAllObservations;

Also, unlike the normal addObserver methods for KVO, these ones return an object that can be stored (assign or weak) by the caller. This object has method, invalidate, which allows the caller to selectively remove a particular binding without affecting any others on the same object.

There is also an invoke method that will force the binding block to execute.

The example project shows a typical use case of binding a model object's properties to a couple of UILabels and using both invalidate and remove* methods to control observation.

Refactor Safe Keypaths

The repo also contains code from Justin Spahr-Summers' libextobjc library to allow refactor safe keypaths. Using a string like @"some.path.to.property" could lead to issues when the property name changes and the compiler is not giving any warnings telling you that your KVO binding just broke.

 [self observe:self keyPath:@keypath(self.value) block:^(ViewController* observed, NSDictionary *change) {
     self.label.text = [NSString stringWithFormat:@"%f", observed.value];
 }];
 
 [self observe:self keyPath:@keypath(self, value) block:^(ViewController* observed, NSDictionary *change) {
     self.label.text = [NSString stringWithFormat:@"%f", observed.value];
 }];
 
 [self observe:self keyPath:@keypath(self, container.value) block:^(MyObject* observed, NSDictionary *change) {
     self.label.text = [NSString stringWithFormat:@"%f", observed.value];
 }];
 
 [self observe:self.container keyPath:@keypath(self.container, value) block:^(MyObject. observed, NSDictionary *change) {
     self.containerLabel.text = [NSString stringWithFormat:@"%f", observed.value];
 }];

License

Contains code from libextobjc

Released under the MIT License. See the LICENSE file for more information.

kvo-block-binding's People

Contributors

b2k avatar hffmnn avatar oskarhagberg avatar rayh avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.